Podemos implementar el SSO entre aplicaciones web tradicionales y aplicaciones no basadas en la web, como los servicios web RESTful. Este ejemplo muestra el código de muestra para implementar el SSO entre la aplicación web y los servicios web RESTful. La siguiente es la configuración en el archivo spring-security.xml
<security:http create-session="never" use-expressions="true"
auto-config="false"
entry-point-ref="preAuthenticatedProcessingFilterEntryPoint" >
<security:intercept-url pattern="/**" access="permitAll"/>
<security:intercept-url pattern="/admin/**" access="hasRole('tomcat')"/>
<security:intercept-url pattern="/**" access="hasRole('tomcat')"/>
<security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthFilter"/>
<!-- Required for Tomcat, will prompt for username/password twice otherwise -->
<security:session-management session-fixation-protection="none"/>
</security:http>
<bean id="preAuthenticatedProcessingFilterEntryPoint"
class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<bean id="preAuthFilter"
class="org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter">
<property name="authenticationManager" ref="appControlAuthenticationManager"/>
<property name="authenticationDetailsSource"
ref="j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource"/>
</bean>
<security:authentication-manager alias="appControlAuthenticationManager">
<security:authentication-provider ref="preAuthenticatedAuthenticationProvider"/>
</security:authentication-manager>
<bean id="preAuthenticatedAuthenticationProvider"
class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<property name="preAuthenticatedUserDetailsService" ref="inMemoryAuthenticationUserDetailsService"/>
</bean>
<bean id="j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource"
class="org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource">
<property name="mappableRolesRetriever" ref="webXmlMappableAttributesRetriever"/>
<property name="userRoles2GrantedAuthoritiesMapper" ref="simpleAttributes2GrantedAuthoritiesMapper"/>
</bean>
<bean id="webXmlMappableAttributesRetriever"
class="org.springframework.security.web.authentication.preauth.j2ee.WebXmlMappableAttributesRetriever"/>
<bean id="simpleAttributes2GrantedAuthoritiesMapper"
class="org.springframework.security.core.authority.mapping.SimpleAttributes2GrantedAuthoritiesMapper">
<property name="attributePrefix" value=""/>
</bean>
<bean id="inMemoryAuthenticationUserDetailsService"
class="com.org.InMemoryAuthenticationUserDetailsService"/>
El código anterior es en la aplicación web. También el mismo código puede estar en el archivo xml de seguridad de primavera del proyecto REST. Agregue el código siguiente en el archivo web.xml
:
<security-constraint>
<web-resource-collection>
<web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
</auth-constraint>
<user-data-constraint>
<!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
El código anterior debe ser sólo en la aplicación web normal. Luego habilite la válvula SSO en el archivo server.xml
de tomcat. Tomcat usa el inicio de sesión SSO basado en cookies. Los identificadores de sesión se almacenan en las cookies. Si su navegador inhabilitó la cookie, entonces SSO no funcionará.
Espero que esta explicación ayude.
Podemos mezclar la autenticación tomcat con la primavera mediante el uso del J2EEPreAutenticationProvider. Yo podría hacer eso. Pero, necesito la implementación de SSO para los servicios REST y otra aplicación web normal – Krishna