当前位置:   article > 正文

Java --- springboot3整合springSecurity_springboot3 security

springboot3 security

一、整合springSecurity

 引入pom依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

config配置类 

  1. @EnableMethodSecurity
  2. @Configuration
  3. public class MySecurityConfig {
  4. @Bean
  5. SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
  6. //请求授权
  7. httpSecurity.authorizeHttpRequests(registry->{
  8. registry.requestMatchers("/").permitAll()//首页都能访问
  9. .anyRequest().authenticated();//除首页外都需要认证
  10. });
  11. //表单登录:使用springSecurity默认表单登录
  12. //httpSecurity.formLogin();
  13. //使用自己的登录
  14. httpSecurity.formLogin(httpSecurityFormLoginConfigurer -> {
  15. httpSecurityFormLoginConfigurer.loginPage("/login").permitAll();//自定义登录页位置
  16. });
  17. return httpSecurity.build();
  18. }
  19. //查询用户详情
  20. @Bean
  21. UserDetailsService userDetailsService(PasswordEncoder passwordEncoder){
  22. UserDetails laowang = User.withUsername("laowang")
  23. .password(passwordEncoder.encode("123456"))//密码必须加密
  24. .roles("admin")
  25. .authorities("file_read")
  26. .build();
  27. UserDetails zhangsan = User.withUsername("zhangsan")
  28. .password(passwordEncoder.encode("123456"))
  29. .roles("admin","hr")
  30. .authorities("file_write")
  31. .build();
  32. //模拟内存中保存所有用户信息
  33. InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(laowang,zhangsan);
  34. return manager;
  35. }
  36. //密码加密器
  37. @Bean
  38. PasswordEncoder passwordEncoder(){
  39. return new BCryptPasswordEncoder();
  40. }
  41. }

前端界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>你好呀</h1>
  9. </body>
  10. </html>
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
  3. <head>
  4. <title>Spring Security Example</title>
  5. </head>
  6. <body>
  7. <div th:if="${param.error}">Invalid username and password.</div>
  8. <div th:if="${param.logout}">You have been logged out.</div>
  9. <form th:action="@{/login}" method="post">
  10. <div>
  11. <label> User Name : <input type="text" name="username" /> </label>
  12. </div>
  13. <div>
  14. <label> Password: <input type="password" name="password" /> </label>
  15. </div>
  16. <div><input type="submit" value="登录" /></div>
  17. </form>
  18. </body>
  19. </html>

controller层

  1. @RestController
  2. public class HelloController {
  3. @GetMapping("/hello")
  4. public String hello(){
  5. return "hello";
  6. }
  7. @PreAuthorize("hasAuthority('file_write')")
  8. @GetMapping("/filed")
  9. public String filed(){
  10. return "获取写权限";
  11. }
  12. }
  1. @Controller
  2. public class LoginController {
  3. @GetMapping("/login")
  4. public String login(){
  5. return "login";
  6. }
  7. }

通过springboot配置文件配置

  1. spring.security.user.name=laowang
  2. spring.security.user.password=123456
  3. spring.security.user.roles=admin,hr

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/388681
推荐阅读
相关标签
  

闽ICP备14008679号