此处是基于spring boot的配置 Pom.xml <dependency> <groupId>com.baomidou</groupId> <artifactId>shaun-spring-boot-starter</artifactId> <version>1.1</version> </dependency> application.properties #开启shaun shaun.annotations.enabled=true #32位字符串,不配置则每次启动不一样导致重启前登录的用户token失效,此处就好比令牌 shaun.salt=3mUyKhbICUow3ArhR2QAdPXJdDuiuygf #默认时true,表示前后端分离 shaun.stateless=true #默认是false,表示不用session shaun.session-on=false #默认header,token的存取位置,此处是header或者cookie或者paramter shaun.token-location=header_or_cookie_or_parameter shaun.header.name=token shaun.cookie.name=token shaun.parameter.name=token #8760个小时 shaun.expire-time=8760h #排除以此路径开头一定要排除登录,否则会报错 shaun.exclude-path=/erp/InventoryUser/userNameAndPassword shaun.exclude-branch=/doc.html,/v2/api-docs,/webjars,/swagger-resources,/swagger-ui.html #shaun.exclude-branch=/swagger-ui.html
controller层
@Autowired private SecurityManager securityManager;
GetMappin("/login") public String login(String username, String password, HttpServletRequest request, HttpServletResponse response) { System.out.println(username + "数据" + password); final TokenProfile profile = new TokenProfile(); if (username != null && password != null) { PasswordHelper passwordHelper = new PasswordHelper(); TUser tUser = itUserService.selectUser(username, passwordHelper.encryptPassword(password)); if (tUser != null) { if (!tUser.getDprtId().equals("")) { try { String dprtIds = itDepartmentService.getDprtChildList(Integer.toString(tUser.getDprtId())); if(StringUtils.isEmpty(dprtIds)) { dprtIds = "-1"; } request.getSession().setAttribute("sysuserdeptids",dprtIds); }catch (Exception e) { /* logger.error("error msg :",e);*/ } // response.addHeader("token",tUser.getDprtId().toString()); profile.setId(tUser.getUserId().toString()); response.addHeader("token",securityManager.login(profile)); return "请求成功"; } } else { return "请求失败"; } } return "请求失败"; } @GetMapping("/loginOut") public CommonResult loginOut() { TokenProfile profile = ProfileHolder.getProfile(); securityManager.logout(profile); return CommonResult.sucess("成功"); } @GetMapping("checkLogin") public CommonResult CheckLogin(){ return CommonResult.sucess("成功"); }