項目集成單點登錄


項目集成單點登錄

1、web.xml集成單點
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  4. <display-name>smartcity base Application</display-name>
  5. <description>smartcity base web</description>
  6. <context-param>
  7. <param-name>casServerLogoutUrl</param-name>
  8. <param-value>http://192.168.21.46:8080/cas/logout</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
  12. </listener>
  13. <filter>
  14. <filter-name>CAS Single Sign Out Filter</filter-name>
  15. <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>CAS Single Sign Out Filter</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>
  21. <!-- CAS 客戶端配置 這個filter負責對請求進行登錄驗證攔截,-->
  22. <filter>
  23. <filter-name>CAS Authentication Filter</filter-name>
  24. <filter-class>
  25. cn.com.bmsoft.smartcity.survey.filter.AuthenticationFilterWithExcludeUrl
  26. </filter-class>
  27. <!-- CAS驗證服務器地址,有域名填寫域名 -->
  28. <init-param>
  29. <param-name>casServerLoginUrl</param-name>
  30. <param-value>
  31. http://192.168.21.46:8080/cas
  32. </param-value>
  33. </init-param>
  34. <init-param>
  35. <param-name>renew</param-name>
  36. <param-value>false</param-value>
  37. </init-param>
  38. <init-param>
  39. <param-name>gateway</param-name>
  40. <param-value>false</param-value>
  41. </init-param>
  42. <init-param>
  43. <param-name>serverName</param-name>
  44. <param-value>http://localhost:8080</param-value>
  45. </init-param>
  46. <init-param>
  47. <description>排除路徑</description>
  48. <param-name>excludePaths</param-name>
  49. <param-value>/management/questionnaire/*,/management/stat/*,/account/*,/resources/*</param-value>
  50. </init-param>
  51. </filter>
  52. <filter-mapping>
  53. <filter-name>CAS Authentication Filter</filter-name>
  54. <url-pattern>/*</url-pattern>
  55. </filter-mapping>
  56. <!--負責Ticket校驗 這個filter負責對請求參數ticket進行驗證(ticket參數是負責子系統與CAS進行驗證交互的憑證)-->
  57. <filter>
  58. <filter-name>CAS Validation Filter</filter-name>
  59. <filter-class>
  60. org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
  61. </filter-class>
  62. <init-param>
  63. <param-name>casServerUrlPrefix</param-name>
  64. <param-value>http://192.168.21.46:8080/cas/</param-value>
  65. </init-param>
  66. <init-param>
  67. <param-name>serverName</param-name>
  68. <param-value>http://localhost:8080</param-value>
  69. </init-param>
  70. <init-param>
  71. <param-name>useSession</param-name>
  72. <param-value>true</param-value>
  73. </init-param>
  74. <init-param>
  75. <param-name>redirectAfterValidation</param-name>
  76. <param-value>true</param-value>
  77. </init-param>
  78. <init-param>
  79. <param-name>encoding</param-name>
  80. <param-value>UTF-8</param-value>
  81. </init-param>
  82. </filter>
  83. <filter-mapping>
  84. <filter-name>CAS Validation Filter</filter-name>
  85. <url-pattern>/*</url-pattern>
  86. </filter-mapping>
  87. <filter>
  88. <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
  89. <filter-class>
  90. org.jasig.cas.client.util.HttpServletRequestWrapperFilter
  91. </filter-class>
  92. </filter>
  93. <filter-mapping>
  94. <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
  95. <url-pattern>/*</url-pattern>
  96. </filter-mapping>
  97. <filter>
  98. <filter-name>CAS Assertion Thread Local Filter</filter-name>
  99. <filter-class>
  100. org.jasig.cas.client.util.AssertionThreadLocalFilter
  101. </filter-class>
  102. </filter>
  103. <filter-mapping>
  104. <filter-name>CAS Assertion Thread Local Filter</filter-name>
  105. <url-pattern>/*</url-pattern>
  106. </filter-mapping>
  107. <!--<filter-mapping>-->
  108. <!--<filter-name>login</filter-name>-->
  109. <!--<url-pattern>/*</url-pattern>-->
  110. <!--</filter-mapping>-->
  111. <listener>
  112. <listener-class>
  113. org.springframework.web.context.request.RequestContextListener
  114. </listener-class>
  115. </listener>
  116. <!--<filter>-->
  117. <!--<filter-name>login</filter-name>-->
  118. <!--<filter-class>cn.com.bmsoft.smartcity.common.LoginFilter</filter-class>-->
  119. <!--<init-param>-->
  120. <!--<param-name>loginUrl</param-name>-->
  121. <!--&lt;!&ndash;下面是未登錄跳轉和超時跳轉代理頁面&ndash;&gt;-->
  122. <!--<param-value>/account/login</param-value>-->
  123. <!--</init-param>-->
  124. <!--</filter>-->
  125. <!--<filter-mapping>-->
  126. <!--<filter-name>login</filter-name>-->
  127. <!--<url-pattern>/*</url-pattern>-->
  128. <!--</filter-mapping>-->
  129. <servlet>
  130. <servlet-name>smartcity springMvc </servlet-name>
  131. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  132. <init-param>
  133. <param-name>contextConfigLocation</param-name>
  134. <param-value>classpath*:servlet-context.xml,classpath*:spring-mybatis.xml,classpath*:spring-rbac.xml</param-value>
  135. </init-param>
  136. <load-on-startup>1</load-on-startup>
  137. </servlet>
  138. <servlet-mapping>
  139. <servlet-name>smartcity springMvc</servlet-name>
  140. <url-pattern>/</url-pattern>
  141. </servlet-mapping>
  142. <session-config>
  143. <session-timeout>60</session-timeout>
  144. </session-config>
  145. <welcome-file-list>
  146. <welcome-file>/</welcome-file>
  147. </welcome-file-list>
  148. </web-app>
2、權限和單點配置信息
  1. sso.enable=true
  2. rbac.enable=true
  3. rbac.url=http://192.168.21.46:8080/
  4. weixin.url=http://192.168.28.111:8181/
  5. sso.casServerUrlPrefix = http://192.168.21.46:8080/cas/
  6. sso.logoutUrl = ${sso.casServerUrlPrefix}logout
  7. sso.client.serverName=http://localhost:8080
3、登錄攔截filter,將單點系統存放在session中的用戶信息拿到后做處理再添加系統所需用戶信息在session中
  1. publicfinalvoid doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)throwsIOException,ServletException{
  2. HttpServletRequest request =(HttpServletRequest)servletRequest;
  3. HttpServletResponse response =(HttpServletResponse)servletResponse;
  4. HttpSession session = request.getSession(false);
  5. Assertion assertion = session !=null?(Assertion)session.getAttribute("_const_cas_assertion_"):null;
  6. if(assertion !=null){
  7. String username = assertion.getPrincipal().getName();
  8. IUserService userService =(IUserService)ServiceLocator.getService("userService");
  9. Map<String,Object> queryParams =newHashMap<String,Object>();
  10. queryParams.put("username",username);
  11. List<User> users = userService.find(queryParams);
  12. SessionUtil.set(request,SessionUtil.USER_SESSION_NAME,users.get(0));
  13. filterChain.doFilter(request, response);
  14. }else{
  15. String serviceUrl =this.constructServiceUrl(request, response);
  16. boolean isInWhiteList =false;
  17. if(excludePaths!=null&& excludePaths.size()>0&& serviceUrl !=null){
  18. for(String path : excludePaths){
  19. if(CommonUtils.isNotBlank(path)){
  20. isInWhiteList =StringUtil.isIn(path,serviceUrl);
  21. if(isInWhiteList){
  22. break;
  23. }
  24. }
  25. }
  26. }
  27. String ticket =CommonUtils.safeGetParameter(request,this.getArtifactParameterName());
  28. boolean wasGatewayed =this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);
  29. if(isInWhiteList){
  30. filterChain.doFilter(request, response);
  31. }elseif(!CommonUtils.isNotBlank(ticket)&&!wasGatewayed){
  32. this.log.debug("no ticket and no assertion found");
  33. String modifiedServiceUrl;
  34. if(this.gateway){
  35. this.log.debug("setting gateway attribute in session");
  36. modifiedServiceUrl =this.gatewayStorage.storeGatewayInformation(request, serviceUrl);
  37. }else{
  38. modifiedServiceUrl = serviceUrl;
  39. }
  40. if(this.log.isDebugEnabled()){
  41. this.log.debug("Constructed service url: "+ modifiedServiceUrl);
  42. }
  43. String urlToRedirectTo =CommonUtils.constructRedirectUrl(this.casServerLoginUrl,this.getServiceParameterName(), modifiedServiceUrl,this.renew,this.gateway);
  44. if(this.log.isDebugEnabled()){
  45. this.log.debug("redirecting to \""+ urlToRedirectTo +"\"");
  46. }
  47. response.sendRedirect(urlToRedirectTo);
  48. }else{
  49. filterChain.doFilter(request, response);
  50. }
  51. }
  52. }
4、修改我們的首頁控制器,獲取session中的用戶信息,處理存放到變量集合
  1. package cn.com.bmsoft.smartcity.common.controller;
  2. import cn.com.bmsoft.smartcity.common.IContextService;
  3. import cn.com.bmsoft.smartcity.common.TreeModel;
  4. import cn.com.bmsoft.smartcity.common.domain.User;
  5. import cn.com.bmsoft.smartcity.common.util.SessionUtil;
  6. import com.bmsoft.jasig.cas.client.util.CASClientUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.servlet.ModelAndView;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. @Controller("home")
  17. publicclassHomeController{
  18. @Autowired
  19. privateIContextService contextService;
  20. @RequestMapping(value={"","/home/index","/home"})
  21. publicModelAndView home(HttpServletRequest request){
  22. Map<String,String> map=newHashMap<String,String>();
  23. User user =(User)SessionUtil.get(request,SessionUtil.USER_SESSION_NAME);
  24. map.put("userName", user.getName());
  25. returnnewModelAndView("home/index",map);
  26. }
  27. @ResponseBody
  28. @RequestMapping(value ={"/home/menu"}, method =RequestMethod.GET)
  29. publicTreeModel getMenu(HttpServletRequest request){
  30. User user =(User)SessionUtil.get(request,SessionUtil.USER_SESSION_NAME);
  31. returnthis.contextService.getMenu(user.getUsername());
  32. }
  33. }
5、退出控制器,退出按鈕方法修改
  1. package cn.com.bmsoft.smartcity.common.controller;
  2. import cn.com.bmsoft.smartcity.common.util.SessionUtil;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.view.RedirectView;
  8. import javax.servlet.http.HttpServletRequest;
  9. /**
  10. * Created by Administrator on 2016/4/18.
  11. */
  12. @Controller
  13. @RequestMapping(value ="/accounts")
  14. publicclassLogoutController{
  15. @Value("${sso.logoutUrl}")
  16. privateString logoutUrl;
  17. @Value("${sso.client.serverName}")
  18. privateString serverName;
  19. // @RequestMapping("/logout")
  20. // public ModelAndView logout(ModelAndView view, HttpServletRequest request) {
  21. // SessionUtil.destroy(request,SessionUtil.USER_SESSION_NAME);
  22. //// request.getSession().invalidate();
  23. // request.setAttribute("message","");
  24. // String logout = logoutUrl+"?service="+serverName+request.getContextPath()+"/";
  25. // view.setView(new RedirectView(logout, false));
  26. // return view;
  27. // }
  28. @RequestMapping("/logout")
  29. publicString logout(HttpServletRequest request){
  30. SessionUtil.destroy(request,SessionUtil.USER_SESSION_NAME);
  31. // request.getSession().invalidate();
  32. request.setAttribute("message","");
  33. return"account/login";
  34. }
  35. }
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM