目标:用户Login一次之后,可以访问同一Server上的不同Webapp, 具体实现上采用Tomcat的Single Sign-On实现. 主要分为下面几个步骤:
这里有2个要点:
- 修改Tomcat conf/server.xml 打开SSO支持
<Host> 节点下增加一个Value节点 <Valve className="org.apache.catalina.authenticator.SingleSignOn" debug="0" requireReauthentication="false"/> </Host>
- container认证realm: user、role、server.xml的<Realm...>设置.
- user 是区别一个个用户的唯一识别了。
- role 就是一些抽象的权限级别,比如“admin”、“manager”、“member”、“guest”等等,都是可以自己定义的.一个user可以拥有多种role.
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99" driverName="your.jdbc.driver.here" connectionURL="your.jdbc.url.here" connectionName="test" connectionPassword="test" userTable="users" userNameCol="user_name" userCredCol="user_pass" userRoleTable="user_roles" roleNameCol="role_name" />
- webapp使用SSO:
- 告诉tomcat这个webapp要通过container的认证
具体做法: 在web.xml里面加上如下的配置: <security-constraint> <web-resource-collection> <web-resource-name>Protected Webapp Example</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <!-- role name 指定哪个role可以访问,可以为多个role --> <role-name>intrauser</role-name> </auth-constraint> </security-constraint>
- 选择一种认证方法
在web.xml里面加上如下的配置: <login-config> <auth-method>BASIC</auth-method> <realm-name>Intra Web Application</realm-name> </login-config> <security-role> <description>The role that is required to access intrasites</description> <role-name>intrauser</role-name> </security-role>
- auth-method
- realm-name
- 如何取得当前的User信息
String userid = request.gerRemoteUser();
以上是在一个Tomcat Container上的SSO实现.
如果是不同的Container上的webapp要做SSO,这种时候一种可行的方案是,最前面架一个webserver(比如apache),在webserver这层承担SSO的认证任务,后面内部就可用挂多个container了. 具体都用到的时候再调查吧.