cas shrio 不同系统单点登陆

不同系统使用不同的登陆名和账号,在做cas中心验证 使用数据库验证就比较纠结,因为登录名和密码两边不一样,后来请教大神,两个系统使用相同的登录名,密码可以不一样,然后在cas的
验证db 设置两个系统的数据源只要一个验证通过就把用户信息放到seession,完成登陆。

参考:https://github.com/coder-huang/sso-shiro-cas

deployerConfigContext.xml

     <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
     <entry key-ref="primaryAuthenticationHandler1" value-ref="primaryPrincipalResolver1" />

   <!-- 通过数据库验证身份, hr 人事系统的登陆 -->
<bean id="primaryAuthenticationHandler"
      class="com.distinct.cas.jdbc.QueryDatabaseAuthenticationHandler"
      p:dataSource-ref="dataSource"
      p:passwordEncoder-ref="passwordEncoder"
      p:sql="select usr_pwd from users where Usr_LoginID=?" />
   

<!-- 通过数据库验证身份,培训  系统的登陆 -->
<bean id="primaryAuthenticationHandler1"
      class="com.distinct.cas.jdbc.QueryDatabaseAuthenticationHandler"
      p:dataSource-ref="dataSource1"
      p:passwordEncoder-ref="passwordEncoder"
      p:sql="select usr_pwd from users where Usr_LoginID=? " />
     


     <!-- 设置数据源hr -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://127:3306/db_hr?useUnicode=true&amp;characterEncoding=utf8"></property>
  <property name="username" value="db_hr_user"></property>
  <property name="password" value="123456"></property> 
    </bean>
   
   
        <!-- 设置数据源traning -->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://128:3306/db_training?useUnicode=true&amp;characterEncoding=utf8"></property>
  <property name="username" value="db_trainning"></property>
  <property name="password" value="123456"></property> 
    </bean>


UserRealm.java

/**
* 1、CAS认证 ,验证用户身份,培训系统 和 hr 系统采用相同的登录名,进行关联
* 2、将用户基本信息设置到会话中
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {

AuthenticationInfo authc = super.doGetAuthenticationInfo(token);
String account = (String) authc.getPrincipals().getPrimaryPrincipal();

User user = this.loginDao.getUser(account);
if(user!=null){
List<UserResource> userResources=this.loginDao.getAllMenusByUser(user);
SecurityUtils.getSubject().getSession().setAttribute(Constants.USER_RESOURCE, userResources);
}
SecurityUtils.getSubject().getSession().setAttribute(Constants.SESSION_USER, user);

return authc;
}
   

猜你喜欢

转载自a545807638.iteye.com/blog/2354971
今日推荐