提交 | 用户 | 时间
|
71e81e
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.modular.system.controller; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
19 |
import cn.stylefeng.guns.base.auth.exception.AuthException; |
|
20 |
import cn.stylefeng.guns.base.auth.exception.enums.AuthExceptionEnum; |
|
21 |
import cn.stylefeng.guns.base.auth.jwt.JwtTokenUtil; |
|
22 |
import cn.stylefeng.guns.base.auth.jwt.payload.JwtPayLoad; |
|
23 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
24 |
import cn.stylefeng.guns.sys.core.util.DefaultImages; |
|
25 |
import cn.stylefeng.guns.sys.modular.system.service.UserService; |
|
26 |
import cn.stylefeng.roses.core.base.controller.BaseController; |
|
27 |
import io.jsonwebtoken.Claims; |
|
28 |
import org.springframework.beans.factory.annotation.Autowired; |
|
29 |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
|
30 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
31 |
import org.springframework.security.core.userdetails.UserDetails; |
|
32 |
import org.springframework.security.core.userdetails.UserDetailsService; |
|
33 |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
34 |
import org.springframework.stereotype.Controller; |
|
35 |
import org.springframework.ui.Model; |
|
36 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
37 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
38 |
import org.springframework.web.bind.annotation.RequestParam; |
|
39 |
|
|
40 |
import javax.servlet.http.Cookie; |
|
41 |
import javax.servlet.http.HttpServletResponse; |
|
42 |
import java.util.List; |
|
43 |
import java.util.Map; |
|
44 |
|
|
45 |
import static cn.stylefeng.guns.base.consts.ConstantsContext.getJwtSecretExpireSec; |
|
46 |
import static cn.stylefeng.guns.base.consts.ConstantsContext.getTokenHeaderName; |
|
47 |
|
|
48 |
/** |
|
49 |
* 单点登录控制器 |
|
50 |
* |
|
51 |
* @author fengshuonan |
|
52 |
* @Date 2017年1月10日 下午8:25:24 |
|
53 |
*/ |
|
54 |
@Controller |
|
55 |
public class SsoLoginController extends BaseController { |
|
56 |
|
|
57 |
@Autowired |
|
58 |
private UserService userService; |
|
59 |
|
|
60 |
@Autowired |
|
61 |
private UserDetailsService userDetailsService; |
|
62 |
|
|
63 |
/** |
|
64 |
* 单点登录入口 |
|
65 |
* |
|
66 |
* @author fengshuonan |
|
67 |
* @Date 2018/12/23 5:42 PM |
|
68 |
*/ |
|
69 |
@RequestMapping(value = "/ssoLogin", method = RequestMethod.GET) |
|
70 |
public String ssoLogin(@RequestParam("token") String token, HttpServletResponse response, Model model) { |
|
71 |
|
|
72 |
//登录并创建token |
|
73 |
Claims claimFromToken = JwtTokenUtil.getClaimFromToken(token); |
|
74 |
System.out.println(claimFromToken); |
|
75 |
Long accountId = (Long) claimFromToken.get("accountId"); |
|
76 |
System.out.println(accountId); |
|
77 |
|
|
78 |
//记录登录日志 |
|
79 |
//LogManager.me().executeLog(LogTaskFactory.loginLog(Long.valueOf(accountId), getIp())); |
|
80 |
|
|
81 |
String token1 = JwtTokenUtil.generateToken(new JwtPayLoad(1L, "admin", "1")); |
|
82 |
|
|
83 |
//创建token |
|
84 |
Cookie authorization = new Cookie(getTokenHeaderName(), token1); |
|
85 |
authorization.setMaxAge(getJwtSecretExpireSec().intValue()); |
|
86 |
authorization.setHttpOnly(true); |
|
87 |
response.addCookie(authorization); |
|
88 |
|
|
89 |
UserDetails userDetails; |
|
90 |
try { |
|
91 |
userDetails = userDetailsService.loadUserByUsername("admin"); |
|
92 |
} catch (UsernameNotFoundException e) { |
|
93 |
throw new AuthException(AuthExceptionEnum.LOGIN_EXPPIRED); |
|
94 |
} |
|
95 |
|
|
96 |
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( |
|
97 |
userDetails, null, userDetails.getAuthorities()); |
|
98 |
SecurityContextHolder.getContext().setAuthentication(authentication); |
|
99 |
|
|
100 |
//获取当前用户角色列表 |
|
101 |
LoginUser user = LoginContextHolder.getContext().getUser(); |
|
102 |
List<Long> roleList = user.getRoleList(); |
|
103 |
|
|
104 |
if (roleList == null || roleList.size() == 0) { |
|
105 |
model.addAttribute("tips", "该用户没有角色,无法登陆"); |
|
106 |
return "/login.html"; |
|
107 |
} |
|
108 |
|
|
109 |
List<Map<String, Object>> menus = userService.getUserMenuNodes(roleList); |
|
110 |
model.addAttribute("menus", menus); |
|
111 |
model.addAttribute("avatar", DefaultImages.defaultAvatarUrl()); |
|
112 |
model.addAttribute("name", user.getName()); |
|
113 |
|
|
114 |
return "/index.html"; |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* 单点登录退出 |
|
119 |
* |
|
120 |
* @author fengshuonan |
|
121 |
* @Date 2018/12/23 5:42 PM |
|
122 |
*/ |
|
123 |
@RequestMapping(value = "/ssoLogout") |
|
124 |
public String ssoLogout(@RequestParam("token") String token, HttpServletResponse response, Model model) { |
|
125 |
return null; |
|
126 |
} |
|
127 |
|
|
128 |
} |