security 概念

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 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )