1.SpringSecurity安全框架
Spring Security是一个功能强大且灵活的安全框架,它专注于为Java应用程序提供身份验证(Authentication)、授权(Authorization)和其他安全功能。Spring Security可以轻松地集成到Spring框架中,为应用程序提供全面的安全性,包括但不限于以下功能:
-
身份验证(Authentication):Spring Security支持多种身份验证方式,如基于表单的身份验证、基于HTTP基本认证、基于OAuth2等。它可以轻松地集成到现有的用户认证系统中,也可以自定义认证逻辑。
-
授权(Authorization):Spring Security允许您定义资源的访问控制规则,以控制哪些用户有权访问哪些资源。您可以使用注解或配置来定义授权规则,从而实现细粒度的权限控制。
-
会话管理:Spring Security支持会话管理,可以处理会话超时、并发登录控制等问题,确保用户会话的安全性。
-
CSRF(Cross-Site Request Forgery)保护:Spring Security可以防止跨站请求伪造攻击,保护应用程序免受此类攻击。
-
记住我(Remember Me):Spring Security提供了"记住我"功能,允许用户在下次访问时保持登录状态。
-
注销(Logout):Spring Security可以处理用户注销操作,包括清除会话信息、退出登录等。
-
安全事件和日志:Spring Security提供了安全事件监听器和日志,可以记录安全事件,便于监控和审计。
-
OAuth2支持:Spring Security对OAuth2协议提供了强大的支持,可以轻松实现OAuth2认证和授权。
1.1 SpringSecurity配置类
过编写配置类,可以定义身份验证方式、授权规则、会话管理等安全相关的设置
2.前后端不分离实现
3.前后端分离实现
3.1 身份验证
3.1.1 service层
将提交的账号密码封装成authentication对象,然后通过认证管理器进行认证
@Slf4j
@Service
public class UserServiceImpl implements UserService {
//认证管理器
@Resource
private AuthenticationManager authenticationManager;
@Resource
private UserMapper userMapper;
@Override
public User findByAccount(String account, String password) {
//将账号密码封装成token对象
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(account,password);
//调用security认证流程
//只要此处得到Authentication就说明登陆成功
Authentication authenticate = authenticationManager.authenticate(token);
//获取user信息
System.out.println(authenticate.getPrincipal());
User user = (User) authenticate.getPrincipal();
if(authenticate==null){
log.debug("登陆失败");
return null;
}else{
log.debug("登陆成功");
return user;
}
}
@Override
public User findById(int id) {
return userMapper.findById(id);
}
}
3.1.2 controller层
将用户信息返回给前端,将token、refreshtoken通过响应头返回给前端
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@PostMapping("/login")
public ResponseResult<User> login(@RequestBody LoginVo loginVo, HttpSession session, HttpServletResponse response){
User user = userService.findByAccount(loginVo.getAccount(),loginVo.getPassword());
DigestUtils.md5DigestAsHex(loginVo.getPassword().getBytes()).equals(user.getPassword())){
//登陆成功
//生成Token令牌
String token = JWTUtil.generateToken(user.getId());
//生成refreshToken
String refreshtoken = UUID.randomUUID().toString();
//放到redis中
redisTemplate.opsForValue().set(refreshtoken,token,JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
//将token放到响应头中返回给前端(流行做法)
response.setHeader("authorization",token);
response.setHeader("refreshtoken",refreshtoken);
//暴露头,浏览器不认识自定义的头,如果不暴露浏览器会自动屏蔽
response.setHeader("Access-Control-Expose-Headers","authorization,refreshtoken");
return new ResponseResult<>(200,"登陆成功",user);
}
}
3.2 鉴权
UsernamePasswordAuthenticationFilter之前手动的将authentication对象放到上下文中
3.2.1 创建过滤器继承OncePerRequestFilter
@Component
public class CustomAuthenticationFilter extends OncePerRequestFilter {
@Resource
private UserService userService;
@Resource
private RedisTemplate redisTemplate;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
// 获取token、refreshtoken
String token = request.getHeader("authorization");//Authentication
String refreshtoken = request.getHeader("refreshtoken");
if (token != null && token.length() !=0 ){
// 校验refreshtoken、token
// 校验refreshtoken:redis中是否有这个key token是否为空 验证token是否与redis一致
if (refreshtoken == null || !redisTemplate.hasKey(refreshtoken) || token == null || JWTUtil.verify(token) == TokenEnum.TOKEN_BAD || !token.equals(redisTemplate.opsForValue().get(refreshtoken))){
// 非法、过期 去登录
extracted(servletResponse);
return;
}
// refreshtoken合法、有token、token合法且与redis一致 得到用户id放到session中
request.getSession().setAttribute("uid", JWTUtil.getuid(token));
// 如果过期
if(JWTUtil.verify(token) == TokenEnum.TOKEN_EXPIRE){
// 过期,重新生成token
token = JWTUtil.generateToken(JWTUtil.getuid(token));
// 修改redis中的数据
redisTemplate.opsForValue().set(refreshtoken, token,JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
// 将新的token返回给前端
HttpServletResponse response = (HttpServletResponse)servletResponse;
response.setHeader("authorization", token);
response.setHeader("Access-Control-Expose-Headers","authorization");
}
// 获取当前用户的id
int uid = JWTUtil.getuid(token);
// 通过用户id查询当前用户的角色、权限信息
User user = userService.findById(uid);
// 将用户信息封装成Authentication对象
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
// 将Authentication对象放到上下文
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
filterChain.doFilter(request, servletResponse);
}
private static void extracted(ServletResponse servletResponse) throws IOException {
ResponseResult<Object> responseResult = new ResponseResult<>(403,"无法访问此界面,请登录",null);
//转json
String json = new ObjectMapper().writeValueAsString(responseResult);
//设置响应头
servletResponse.setContentType("application/json;charset=utf-8");
servletResponse.getWriter().write(json);
}
}
3.2.2 在配置类中将过滤器放在UsernamePasswordAuthenticationFilter之前
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
3.3 异常处理
在Spring Security中,异常处理是处理安全相关的异常情况,例如认证失败、访问拒绝等
1.当用户未登录或者认证失败时,Spring Security 会调用 AuthenticationEntryPoint
的实现来处理该异常,并返回适当的响应给客户端
2.当用户提供的凭据不正确或者认证失败时,Spring Security会抛出BadCredentialsException异常。可以通过实现AuthenticationFailureHandler接口来自定义认证失败的处理逻辑
3.当用户访问了没有权限的资源时,Spring Security会抛出AccessDeniedException异常。可以通过实现AccessDeniedHandler接口来自定义访问拒绝的处理逻辑
3.3.1 未登录异常
@Component
@RestControllerAdvice
public class CustomNologinExceptionHandler implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8");
httpServletResponse.getWriter().write(
new ObjectMapper().writeValueAsString(
new ResponseResult<>(403,"你没有登录",false)));
}
}
3.3.1.1 配置类中处理未登录规则
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.authenticationEntryPoint(customNoLoginExceptionHandler)// 用户没登录怎么处理
}
3.3.2 账号密码有误异常
全局异常处理:一种在应用程序中统一处理异常的机制,它能够捕获应用程序中抛出的所有异常,并通过统一的处理逻辑进行处理,以便更好地向用户返回错误信息或执行其他操作
注:全局异常处理捕获不到security中报的某些异常
@Slf4j
@RestControllerAdvice
public class AuthenticationExceptionHandler {
@ExceptionHandler(BadCredentialsException.class)
public ResponseResult<Boolean> handler(BadCredentialsException e){
log.debug(e.getClass()+"");
return new ResponseResult<>(403,"账号或密码有误",false);
}
}
3.3.3 无权限异常
@Component
@RestControllerAdvice
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, org.springframework.security.access.AccessDeniedException e) throws IOException, ServletException {
httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8");
httpServletResponse.getWriter().write(
new ObjectMapper().writeValueAsString(
new ResponseResult<>(401,"你没有权限",false)));
}
}
3.3.3.1 配置类中处理没权限规则
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.exceptionHandling() // 指定异常处理
.accessDeniedHandler(customAccessDeniedHandler) // 没权限怎么处理
}
3.4 解决swagger冲突问题
3.4.1 在配置类中进行放行swagger静态资源
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/swagger-ui.html","/webjars/**","/v2/**","/swagger-resources/**")
.permitAll() //不需要认证就能访问
.anyRequest().authenticated() // 需要认证
}