当前位置:   article > 正文

Spring boot 3 (3.1.5) Spring Security 设置一_spring boot 3.1.5

spring boot 3.1.5

项目依赖中添加:

testImplementation 'org.springframework.security:spring-security-test'

创建Security设置文件:

SecutiryConfig.java

  1. import com.example.sino.utils.JWTFilter;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  9. import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
  10. import org.springframework.security.web.SecurityFilterChain;
  11. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  12. @EnableWebSecurity
  13. @Configuration
  14. public class SecurityConfig {
  15. @Autowired
  16. private JWTFilter jwtFilter;
  17. @Bean
  18. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  19. http.authorizeHttpRequests(
  20. authorize -> authorize
  21. .requestMatchers("/api/welcome").permitAll() // 公开访问
  22. .requestMatchers("/api/admin").hasAuthority("ADMIN") // ADMIN权限
  23. .anyRequest().authenticated() // 其它必须登录才能访问
  24. );
  25. // Spring Security 6 中默认是关闭登录表单的,这里如果添加以下代码则是开启登录表单。
  26. // 开启登录表单
  27. // http.formLogin(Customizer.withDefaults());
  28. // 禁用csrf
  29. http.csrf(AbstractHttpConfigurer::disable);
  30. // 验证token
  31. http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
  32. return http.build();
  33. }
  34. }

测试

创建两个路由 /api/welcome 和 /api/admin。分别访问。

/api/welcome 直接就能看到内容, /api/admin 则返回401

  1. import com.example.sino.domain.JsonResult;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @RequestMapping("/api")
  7. public class WelcomeController {
  8. @RequestMapping("welcome")
  9. public JsonResult index() {
  10. return JsonResult.success("Welcome!");
  11. }
  12. @RequestMapping("admin")
  13. public JsonResult admin() {
  14. return JsonResult.success("Hi admin.");
  15. }
  16. }

附录:

JsonResult代码

  1. import java.util.HashMap;
  2. public class JsonResult extends HashMap<String, Object> {
  3. // 业务状态码
  4. public static final String CODE_TAG = "code";
  5. // 消息
  6. public static final String MSG_TAG = "msg";
  7. // 数据
  8. public static final String DATA_TAG = "data";
  9. public JsonResult() {
  10. }
  11. public JsonResult(int code, String msg) {
  12. super.put(CODE_TAG, code);
  13. super.put(MSG_TAG, msg);
  14. }
  15. public JsonResult(int code, String msg, Object data) {
  16. super.put(CODE_TAG, code);
  17. super.put(MSG_TAG, msg);
  18. if (data != null) {
  19. super.put(DATA_TAG, data);
  20. }
  21. }
  22. /**
  23. * 返回成功消息
  24. *
  25. * @return 成功消息
  26. */
  27. public static JsonResult success() {
  28. return JsonResult.success("操作成功");
  29. }
  30. /**
  31. * 返回成功数据
  32. *
  33. * @return 成功消息
  34. */
  35. public static JsonResult success(Object data) {
  36. return JsonResult.success("操作成功", data);
  37. }
  38. /**
  39. * 返回成功消息
  40. *
  41. * @param msg 返回内容
  42. * @return 成功消息
  43. */
  44. public static JsonResult success(String msg) {
  45. return JsonResult.success(msg, null);
  46. }
  47. /**
  48. * 返回成功消息
  49. *
  50. * @param msg 返回内容
  51. * @param data 数据对象
  52. * @return 成功消息
  53. */
  54. public static JsonResult success(String msg, Object data) {
  55. return new JsonResult(200, msg, data);
  56. }
  57. /**
  58. * 返回错误消息
  59. *
  60. * @return 错误消息
  61. */
  62. public static JsonResult error() {
  63. return JsonResult.error("操作失败");
  64. }
  65. /**
  66. * 返回错误消息
  67. *
  68. * @param msg 返回内容
  69. * @return 错误消息
  70. */
  71. public static JsonResult error(String msg) {
  72. return JsonResult.error(msg, null);
  73. }
  74. /**
  75. * 返回错误消息
  76. *
  77. * @param msg 返回内容
  78. * @param data 数据对象
  79. * @return 错误消息
  80. */
  81. public static JsonResult error(String msg, Object data) {
  82. return new JsonResult(500, msg, data);
  83. }
  84. /**
  85. * 方便链式调用
  86. *
  87. * @param key 键
  88. * @param value 值
  89. * @return 数据对象
  90. */
  91. @Override
  92. public JsonResult put(String key, Object value) {
  93. super.put(key, value);
  94. return this;
  95. }
  96. }

JWTFilter.java代码见下期,还没整明白。

-完-

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

闽ICP备14008679号