本文主要是介绍spring boot 单纯的使用cas client配置详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring boot配置单点登录客户端配置步骤
1) pom.xml引入jar包:
<!-- cas client -->
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.2.0-GA</version>
</dependency>
2) 在配置文件中配置cas client中的相关属性值:
############ cas client配置 开始 ############
cas.server-url-prefix = http\://localhost\:8180/cas-server-webapp-3.5.2
cas.server-login-url = http\://localhost\:8180/cas-server-webapp-3.5.2/login
cas.client-host-url = http\://localhost\:9290
cas.validation-type = CAS
#设置可过滤的url地址
cas.authentication-url-patterns[0] = /manager/*
#cas.validation-url-patterns[0] = /manager/*
#cas.request-wrapper-url-patterns[0] = /manager/*
#cas.assertion-thread-local-url-patterns[0] = /manager/*
#其他访问地址配置
cas-server-logout-url = http\://localhost\:8180/cas-server-webapp-3.5.2/logout
app-login-url = http\://localhost\:9290/manager/login/loginIn
############ cas client配置 结束 ############
3) 在启动类中添加cas client注解:
@EnableCasClient//cas client客户端
4) 修改login的登陆方法
@RequestMapping("/manager/login/loginIn")
public ModelAndView loginIn(HttpSession session,
HttpServletRequest request, HttpServletResponse response,
ModelMap modelMap) throws Exception {
//返回的登录用户信息
Object object = request.getSession().getAttribute("_const_cas_assertion_"); //通过cas client获取
if(null != object){
Assertion assertion = (Assertion) object;
String loginName = assertion.getPrincipal().getName();
Map<String,Object> att = assertion.getPrincipal().getAttributes();//获取属性值,为一个Map类型。
System.out.println("登陆用户名"+loginName);
System.out.println("登陆返回的属性"+att);
UserInfo user = UserUtil.getCurrentUser(request.getSession());
if (user == null) {
String passWord = (String) att.get("passWord");
String roleId = (String) att.get("roleId");
String deptCode = (String) att.get("deptCode");
String userCode = (String) att.get("userCode");
String realname = (String) att.get("realname");
String mobile = (String) att.get("mobile");
String weChat = (String) att.get("weChat");
String email = (String) att.get("email");
String id = (String) att.get("id");
user = new UserInfo();
user.setUserName(loginName);
user.setPassWord(passWord);
user.setUserCode(userCode);
user.setRoleId(new Integer(roleId)); //角色ID
user.setDeptCode(deptCode); //部门编码
user.setEmail(email);
user.setRealname(realname);
user.setMobile(mobile);
user.setWeChat(weChat);
user.setId(new Integer(id));
// 将用户信息保存到session
UserUtil.saveUserToSession(session, user);
session.setAttribute(Constants.sessionFactory, this.getSqlSessionFactory());
}
}else{
logger.info("==,登陆失败,返回登录页面");
modelMap.put("error", "true");
return new ModelAndView("/pages/login.jsp");
}
UserInfo user = UserUtil.getCurrentUser(session); //获取当前登录用户信息
//以下是自己根据业务需要的其他代码
return new ModelAndView("/pages/main.jsp");
}
5) 修改login的登出方法:
@RequestMapping("/manager/login/loginOut")
public void loginOut(HttpSession session, HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap) throws Exception {
session.removeAttribute("musersession");
session.removeAttribute("_const_cas_assertion_"); //单点登录的session值去除
session.removeAttribute(Constants.USER_INFO);
session.removeAttribute(Constants.sessionFactory);
session.invalidate();
logger.info("==登陆超时,重新返回首页");
//单点登录登出
String logout_url = cas_server_logout_url + "?service=" + app_login_url;
response.sendRedirect(logout_url);
//return new ModelAndView("/pages/login.jsp"); //spring-boot会自动去src/main/webapp/pages下面找home.html这个文件
}
其中属性值的获取(就在loginCotroller.java中):
//获取properties文件中单点登录的地址
@Value("${cas.server-login-url}")
private String cas_server_login_url; //单点登录的地址
//获取properties文件中单点登出的地址
@Value("${cas-server-logout-url}")
private String cas_server_logout_url; //单点登录的登出地址
//获取properties文件中本系统的登陆地址
@Value("${app-login-url}")
private String app_login_url; //本系统的登陆地址
6)cas-client-autoconfig-support源码地址:https://github.com/Unicon/cas-client-autoconfig-support
注意:引入的jar包的版本号是1.2.0-GA
参照网址:http://blog.csdn.net/liuchuanhong1/article/details/73176603
这篇关于spring boot 单纯的使用cas client配置详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!