Weblogic中通过IdentityAsserter实现SSO
weblogic8实现SSO的一种简单方案,就是通过IdentityAsserter解决。
步骤一、SSO目标配置
<security-constraint> <web-resource-collection> <web-resource-name>Protected Area</web-resource-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.do</url-pattern> <url-pattern>*.html</url-pattern> <url-pattern>*.htm</url-pattern> <url-pattern>*.doc</url-pattern> <url-pattern>*.xls</url-pattern> <url-pattern>*.xlsx</url-pattern> <url-pattern>*.zip</url-pattern> <url-pattern>*.rar</url-pattern> <url-pattern>*.jpg</url-pattern> </web-resource-collection> <auth-constraint> <role-name>secrole</role-name> </auth-constraint></security-constraint><login-config> <auth-method>CLIENT-CERT</auth-method> <realm-name>myrealm</realm-name></login-config><security-role> <role-name>secrole</role-name></security-role>
<?xml version="1.0" ?><!DOCTYPE MBeanType SYSTEM "commo.dtd"><MBeanType Name = "ImepIdentityAsserter" DisplayName = "ImepIdentityAsserter"Package = "com.huawei.netforce.security.sso"Extends = "weblogic.management.security.authentication.IdentityAsserter"PersistPolicy = "OnUpdate"><MBeanAttribute Name = "ProviderClassName" Type = "java.lang.String"Writeable = "false"Default = ""com.huawei.netforce.security.sso.ImepIdentityAsserterProviderImpl""/><MBeanAttribute Name = "Description" Type = "java.lang.String"Writeable = "false" Default = ""ImepIdentityAsserter Identity Assertion Provider""/><MBeanAttribute Name = "Version" Type = "java.lang.String"Writeable = "false" Default = ""1.0""/><MBeanAttribute Name = "SupportedTypes" Type = "java.lang.String[]"Writeable = "false" Default = "new String[] {"ImepToken"}"/><MBeanAttribute Name = "ActiveTypes" Type = "java.lang.String[]"Default = "new String[] {"ImepToken"}"/></MBeanType>
import java.io.PrintStream;import javax.security.auth.callback.CallbackHandler;import javax.security.auth.login.AppConfigurationEntry;import weblogic.management.security.ProviderMBean;import weblogic.security.spi.*;public class ImepIdentityAsserterProviderImpl implements AuthenticationProvider, IdentityAsserter{ private static final String TOKEN_TYPE = "ImepToken"; private static final String TOKEN_PREFIX = "username="; private String description; public ImepIdentityAsserterProviderImpl() { } public AppConfigurationEntry getAssertionModuleConfiguration() { return null; } public IdentityAsserter getIdentityAsserter() { return this; } public AppConfigurationEntry getLoginModuleConfiguration() { return null; } public PrincipalValidator getPrincipalValidator() { return null; } public String getDescription() { return description; } public void initialize(ProviderMBean mbean, SecurityServices services) { System.out.println("ImepIdentityAsserterProviderImpl.initialize"); ImepIdentityAsserterMBean myMBean = (ImepIdentityAsserterMBean)mbean; description = myMBean.getDescription() + "\n" + myMBean.getVersion(); } public void shutdown() { } public CallbackHandler assertIdentity(String type, Object token) throws IdentityAssertionException { if(!"ImepToken".equals(type)) { String error = "ImepIdentityAsserterProviderImpl received unknown token type "" + type + ""." + " Expected " + "ImepToken"; throw new IdentityAssertionException(error); } if(!(token instanceof byte[])) { String error = "ImepIdentityAsserterProviderImpl received unknown token class "" + token.getClass() + ""." + " Expected a byte[]."; throw new IdentityAssertionException(error); } byte tokenBytes[] = (byte[])token; if(tokenBytes == null || tokenBytes.length < 1) { String error = "ImepIdentityAsserterProviderImpl received empty token byte array"; throw new IdentityAssertionException(error); } String tokenStr = new String(tokenBytes); if(!tokenStr.startsWith("username=")) { String error = "ImepIdentityAsserterProviderImpl received unknown token string "" + type + ""." + " Expected " + "username=" + "username"; throw new IdentityAssertionException(error); } else { String userName = tokenStr.substring("username=".length()); return new ImepCallbackHandlerImpl(userName); } }}
import java.io.IOException;import javax.security.auth.callback.*;public class ImepCallbackHandlerImpl implements CallbackHandler{ private String userName; public ImepCallbackHandlerImpl(String aUserName) { userName = aUserName; } public void handle(Callback callbacks[]) throws IOException, UnsupportedCallbackException { for(int i = 0; i < callbacks.length; i++) { Callback callback = callbacks[i]; if(!(callback instanceof NameCallback)) throw new UnsupportedCallbackException(callback, "Unrecognized Callback"); NameCallback nameCallback = (NameCallback)callback; nameCallback.setName(userName); } }}
try { //test账户需要在myrealm中进行配置 String token = "username=test"; //是否需要编码可在控制台配置时取消,默认为BASE64编码 BASE64Encoder encoder = new BASE64Encoder(); String encodedToken = encoder.encodeBuffer(token.getBytes()); URL url = new URL("http://localhost:7001/app/index.jsp"); URLConnection connection = url.openConnection(); connection.setRequestProperty("ImepToken",encodedToken); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = ""; while((line = in.readLine()) != null) { System.out.println(line); } } catch(Exception e) { e.printStackTrace(); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String tokenValue = request.getParameter("token"); String redirect = request.getParameter("redirect"); if(StringUtils.isNotEmpty(tokenValue)) { //IdentityAsserterProvider从cookie或者header中取token Cookie cookie = new Cookie("ImepToken", tokenValue); response.addCookie(cookie); response.setHeader("ImepToken",tokenValue); //添加P3P策略主要解决iframe集成时浏览器阻止跨域cookie response.setHeader("P3P","CP=CAO PSA OUR"); response.sendRedirect(redirect); } }