| | |
| | | # 授权协议 |
| | | 本项目自有代码使用宽松的MIT协议,在保留版权信息的情况下可以自由应用于各自商用、非商业的项目。 但是本项目也零碎的使用了一些其他的开源代码,在商用的情况下请自行替代或剔除; 由于使用本项目而产生的商业纠纷或侵权行为一概与本项目及开发者无关,请自行承担法律风险。 在使用本项目代码时,也应该在授权协议中同时表明本项目依赖的第三方库的协议 |
| | | |
| | | # 付费技术支持 |
| | | # 技术支持 |
| | | 建议加入[知识星球](https://t.zsxq.com/0drbw002x)可以获取更多的教程以及更加及时的回复。 |
| | | 如果项目需要一对一的技术支持,或者棘手的问题需要解决,请发送邮件到648540858@qq.com |
| | | |
| | | # 致谢 |
| | |
| | | |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | | import com.genersoft.iot.vmp.conf.security.dto.JwtUser; |
| | | import com.genersoft.iot.vmp.storager.dao.dto.Role; |
| | | import com.genersoft.iot.vmp.storager.dao.dto.User; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | |
| | | } |
| | | |
| | | // 构建UsernamePasswordAuthenticationToken,这里密码为null,是因为提供了正确的JWT,实现自动登录 |
| | | UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, jwtUser.getPassword(), new ArrayList<>() ); |
| | | User user = new User(); |
| | | user.setUsername(jwtUser.getUserName()); |
| | | user.setPassword(jwtUser.getPassword()); |
| | | Role role = new Role(); |
| | | role.setId(jwtUser.getRoleId()); |
| | | user.setRole(role); |
| | | UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, jwtUser.getPassword(), new ArrayList<>() ); |
| | | SecurityContextHolder.getContext().setAuthentication(token); |
| | | chain.doFilter(request, response); |
| | | } |
| | |
| | | */ |
| | | public static final long expirationTime = 30; |
| | | |
| | | public static String createToken(String username, String password) { |
| | | public static String createToken(String username, String password, Integer roleId) { |
| | | try { |
| | | /** |
| | | * “iss” (issuer) 发行人 |
| | |
| | | //添加自定义参数,必须是字符串类型 |
| | | claims.setClaim("username", username); |
| | | claims.setClaim("password", password); |
| | | claims.setClaim("roleId", roleId); |
| | | |
| | | //jws |
| | | JsonWebSignature jws = new JsonWebSignature(); |
| | |
| | | |
| | | String username = (String) claims.getClaimValue("username"); |
| | | String password = (String) claims.getClaimValue("password"); |
| | | Long roleId = (Long) claims.getClaimValue("roleId"); |
| | | jwtUser.setUserName(username); |
| | | jwtUser.setPassword(password); |
| | | jwtUser.setRoleId(roleId.intValue()); |
| | | |
| | | return jwtUser; |
| | | } catch (InvalidJwtException e) { |
| | |
| | | Authentication authentication = getAuthentication(); |
| | | if(authentication!=null){ |
| | | Object principal = authentication.getPrincipal(); |
| | | if(principal!=null && !"anonymousUser".equals(principal)){ |
| | | // LoginUser user = (LoginUser) authentication.getPrincipal(); |
| | | if(principal!=null && !"anonymousUser".equals(principal.toString())){ |
| | | |
| | | String username = (String) principal; |
| | | User user = new User(); |
| | | user.setUsername(username); |
| | | LoginUser loginUser = new LoginUser(user, LocalDateTime.now()); |
| | | return loginUser; |
| | | User user = (User) principal; |
| | | return new LoginUser(user, LocalDateTime.now()); |
| | | } |
| | | } |
| | | return null; |
| | |
| | | * 登出成功的处理 |
| | | */ |
| | | @Autowired |
| | | private LoginFailureHandler loginFailureHandler; |
| | | /** |
| | | * 登录成功的处理 |
| | | */ |
| | | @Autowired |
| | | private LoginSuccessHandler loginSuccessHandler; |
| | | /** |
| | | * 登出成功的处理 |
| | | */ |
| | | @Autowired |
| | | private LogoutHandler logoutHandler; |
| | | /** |
| | | * 未登录的处理 |
| | |
| | | |
| | | private String password; |
| | | |
| | | private int roleId; |
| | | |
| | | private TokenStatus status; |
| | | |
| | | public String getUserName() { |
| | |
| | | public void setPassword(String password) { |
| | | this.password = password; |
| | | } |
| | | |
| | | public int getRoleId() { |
| | | return roleId; |
| | | } |
| | | |
| | | public void setRoleId(int roleId) { |
| | | this.roleId = roleId; |
| | | } |
| | | } |
| | |
| | | if (user == null) { |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "用户名或密码错误"); |
| | | }else { |
| | | String jwt = JwtUtils.createToken(username, password); |
| | | String jwt = JwtUtils.createToken(username, password, user.getRole().getId()); |
| | | response.setHeader(JwtUtils.getHeader(), jwt); |
| | | user.setAccessToken(jwt); |
| | | } |