• spring security快速入门 (无多余额外功能)


     ps:情人节不得学一手认证鉴权压压惊

    1.导入依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.projectlombokgroupId>
    8. <artifactId>lombokartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-securityartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>mysqlgroupId>
    16. <artifactId>mysql-connector-javaartifactId>
    17. dependency>
    18. <dependency>
    19. <groupId>com.baomidougroupId>
    20. <artifactId>mybatis-plus-boot-starterartifactId>
    21. <version>3.5.1version>
    22. dependency>
    23. dependencies>

    配置数据库连接 yml

    1. server:
    2. port: 8080
    3. spring:
    4. datasource:
    5. driver-class-name: com.mysql.cj.jdbc.Driver
    6. url: jdbc:mysql://localhost:3306/easymybatis?serverTimezone=GMT%2B8
    7. username: root
    8. password: 123456

    2.实现UserDetailsServic接口(自行创建mapper接口)

    数据库结构:

    1. @Service
    2. public class MyUserDetailService implements UserDetailsService {
    3. @Autowired
    4. private UserMapper userMapper;
    5. @Override
    6. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    7. QueryWrapper queryWrapper=new QueryWrapper<>();
    8. queryWrapper.eq("name",s);
    9. MemberUser memberUser = userMapper.selectOne(queryWrapper);
    10. if (memberUser==null){
    11. throw new UsernameNotFoundException("验证失败");
    12. }
    13. List auths= AuthorityUtils.commaSeparatedStringToAuthorityList("role");
    14. return new User(memberUser.getName(),new BCryptPasswordEncoder().encode(memberUser.getPassword()),auths);
    15. }
    16. }

    3.编写相关配置类 继承WebSecurityConfigurerAdapter

    1. @SpringBootConfiguration
    2. public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    3. @Autowired
    4. private MyUserDetailService userDetailService;
    5. @Override
    6. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    7. //super.configure(auth);
    8. //用DetailService 方式鉴权
    9. auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder());
    10. }
    11. //老版不分离自定义登录方案
    12. /* @Override
    13. protected void configure(HttpSecurity http) throws Exception {
    14. http.formLogin()
    15. .loginPage("index.html") //登录页面
    16. .successForwardUrl("success.html") //登录成功跳转
    17. .loginProcessingUrl("/user/login").permitAll() //请求服务器路径
    18. .and().authorizeRequests()
    19. .antMatchers("/","index/html").permitAll() //不需要认证
    20. .antMatchers("/test").hasAnyAuthority("role") //拥有权限才能访问
    21. .anyRequest().authenticated()
    22. .and().csrf().disable();
    23. }
    24. */
    25. //加密措施
    26. @Bean
    27. PasswordEncoder passwordEncoder(){
    28. return new BCryptPasswordEncoder();
    29. }
    30. }

    4.测试

    1. @RestController
    2. @RequestMapping("test")
    3. public class TestController {
    4. @GetMapping("hello")
    5. public String test(){
    6. return "hello security";
    7. }
    8. }

  • 相关阅读:
    Docker技术在测试中的应用--环境搭建篇
    Java面试题大全(整理版)1000+面试题附答案详解,最全面详细,看完稳了
    四大组件---ContentResolver
    使用TensorFlow 2.x实现深度学习模型的完整指南
    VC++判断程序是否已经运行;仅运行一次
    golang 基础-golang里面的读写锁实现与核心原理分析
    Qt通过Doc模式读取XML并设计一个增删改查方便的一个操作类
    读图数据库实战笔记01_初识图
    ADAS/AD笔记之特斯拉Autopilot-HW3.0系统
    高NA镜头系统中的高级PSF计算
  • 原文地址:https://blog.csdn.net/qq_50598935/article/details/126157359