为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Security + Tomcat SSO

Spring Security + Tomcat SSO

翻翻过去那场雪 2019-03-13 18:15:18
请教各位大神,我有多个 Webapps 部署在一个 Tomcat 上。每个 Webapp 都使用 Spring Security 控制机制,进行用户认证和权授。这个后台认证和权授的数据库是统一的。我希望能在这些 webapp 之间实现 SSO – Single Sign On -- 其中一个 Webapp 上登录后,就可以按权限访问其它的 webapp。我现在能实现单个 Webapp 用 Spring Security 的认证和授权控制,但不知道在这种情况下(每个 Webapp 都部署在同一个 Tomcat中)如何实现 SSO?我在 StackOverflow 上也没有找到答案,恳请达人指点。
查看完整描述

2 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

每个 APP 使用 Tomcat JDBCRealm 进行认证 (Authentication),但使用 Spring Security 进行授权。两者基于相同的用户信息数据库。

  1. 在 Tomcat 中打开 SSO -- 这个很重要,否则访问同一个域中其它 webapp 时,不会带上 Cookie,也就无法认证了

  2. 在每个 webapp 中,配置 Web.xml 使用 Tomcat 进行认证 -- 如果用 Spring 进行认证,则 Tomcat 的 SSO 不起作用

  3. 在每个 webapp 中,配置 spring,使用 J2eePreAuthenticatedProcessingFilter,进行权限控制 (Authorization)

spring.xml 中的配置

    <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">

        <constructor-arg name="strength" value="11" />

    </bean>


       <bean id="forbiddenEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>


    <security:http auto-config="false" use-expressions="true" entry-point-ref="forbiddenEntryPoint">

        <security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthFilter"/>

        <security:intercept-url pattern="/index/**" access="hasAnyRole('ROLE_SUPER')" />

        <security:session-management session-fixation-protection="none"/>

        <security:csrf disabled="true"/>

    </security:http>


 

    <bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">

        <property name="throwExceptionWhenTokenRejected" value="true"/>

        <property name="preAuthenticatedUserDetailsService">

           <bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">

            <property name="userDetailsService" ref="nosUserDetailsService" />

        </bean>

        </property>

    </bean>

    



    <bean id="preAuthenticatedProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>


    <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="j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource" class="org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource">

        <property name="mappableRolesRetriever" ref="webXmlMappableAttributesRetriever"/>

        <property name="userRoles2GrantedAuthoritiesMapper" ref="simpleAttributes2GrantedAuthoritiesMapper"/>

    </bean>

    

    <bean id="preAuthFilter" class="org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter">

        <property name="authenticationManager" ref="authenticationManager"/>

        <property name="authenticationDetailsSource" ref="j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource"/>

    </bean>


    <security:authentication-manager alias="authenticationManager">

        <security:authentication-provider ref="preauthAuthProvider"/>

    </security:authentication-manager>


查看完整回答
反对 回复 2019-04-25
  • 2 回答
  • 0 关注
  • 451 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号