用Tomcat 的SSO實現
目標:用戶Login一次之后,可以訪問同一Server上的不同Webapp, 具體實現上采用Tomcat的Single Sign-On實現. 主要分為下面幾個步驟:- 修改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>http://www.bt285.cn BT下載 </web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <!-- role name 指定哪個role可以訪問,可以為多個role,如兩個網站:http://www.5a520.cn http://www.feng123.com -->
<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了. 具體都用到的時候再調查吧.