security
创建核心配置文件
1@EnableWebSecurity 2public class SeurityConfig extends WebSecurityConfigurerAdapter{}
改写方法
1 /** 2 * 认证 3 * 在内存中创建了一个用户 4 * 5 * @param auth 6 * @throws Exception 7 */ 8 @Override 9 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 10 auth.inMemoryAuthentication() 11 .withUser("name") 12 .password(passwordEncoder.encode("123")) 13 .roles("ADMIN");//添加角色 14 } 15
添加密码加密器
1 /** 2 * 给容器加一个密码加密器 3 * 4 * @return 5 */ 6 @Bean 7 public PasswordEncoder passwordEncoder() { 8 return new BCryptPasswordEncoder(); 9 }
密码加密
1 /** 2 * 对一同个字符串进行加密三次 三次得出的结果是不一样的 3 * 只要是用同一个加密器加密的 解密也是一样的 4 * @param args 5 */ 6 public static void main(String[] args) { 7 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); 8 9 //加密 10 System.out.println(bCryptPasswordEncoder.encode("123")); 11 System.out.println(bCryptPasswordEncoder.encode("123")); 12 System.out.println(bCryptPasswordEncoder.encode("123")); 13 14 15 //解密 16 bCryptPasswordEncoder.matches("123","$2a$10$rvN6duVInXYxtuoI.eH53uSOSpgb/mkLVOUQCEfHidHoUnZjxbAf6")); 17 }
获取当前用户的信息
1/** 2 * 获取当前用户 3 * 4 * @param principal 5 * @return 6 */ 7 @GetMapping("getUserInfo") 8 private String getUserInfo(Principal principal) { 9 System.out.println(principal); 10 return ""; 11 } 12 13 /** 14 * 获取当前用户 15 * <p> 16 * 当用户登录完之后 会把用户的消息 放到 17 * SecurityContextHolder 基于session方式的认证 18 * 19 * @return 20 */ 21 @GetMapping("getUserInfo2") 22 private Authentication getUserInfo2() { 23 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 24 return authentication; 25 } 26
登录失败的处理器
1 /** 2 * 登录失败的处理器 3 * 4 * @return 5 */ 6 @Bean 7 public AuthenticationFailureHandler authenticationFailureHandler() { 8 return (rquset, response, exception) -> { 9 response.setContentType("application/json;charset=utf-8"); 10 PrintWriter pw = response.getWriter(); 11 HashMap<String, Object> map = new HashMap<>(4); 12 map.put("code", 401); 13 if (exception instanceof LockedException) { 14 map.put("msg", "账户被锁定,登陆失败!"); 15 } else if (exception instanceof BadCredentialsException) { 16 map.put("msg", "账户或者密码错误,登陆失败!"); 17 } else if (exception instanceof DisabledException) { 18 map.put("msg", "账户被禁用,登陆失败!"); 19 } else if (exception instanceof AccountExpiredException) { 20 map.put("msg", "账户已过期,登陆失败!"); 21 } else if (exception instanceof CredentialsExpiredException) { 22 map.put("msg", "密码已过期,登陆失败!"); 23 } else { 24 map.put("msg", "登陆失败!"); 25 } 26 pw.write(new ObjectMapper().writeValueAsString(map)); 27 pw.flush(); 28 pw.close(); 29 }; 30 }
访问接口被拒绝的处理器
1 /** 2 * 请求被拒绝的处理器 3 * 4 * @return 5 */ 6 @Bean 7 public AccessDeniedHandler accessDeniedHandler() { 8 return (request, response, accessDeniedException) -> { 9 response.setContentType("application/json;charset=utf-8"); 10 HashMap<String, Object> map = new HashMap<>(4); 11 map.put("code", 403); 12 map.put("msg", "你没有权限"); 13 PrintWriter pw = response.getWriter(); 14 pw.write(new ObjectMapper().writeValueAsString(map)); 15 pw.flush(); 16 pw.close(); 17 }; 18 }
