当前位置:   article > 正文

BS问题:Spring boot Admin 配置 security 导致客户端连接 401 : [no body]

401 : [no body]

客户端配置 

  1. spring:
  2. boot:
  3. admin:
  4. client:
  5. url: http://localhost:7000
  6. username: admin
  7. password: admin
  8. instance:
  9. name: admin-client
  10. prefer-ip: true
  11. application:
  12. name: admin-client
  13. management:
  14. endpoint:
  15. health:
  16. show-details: always
  17. endpoints:
  18. enabled-by-default: true
  19. web:
  20. base-path: /actuator
  21. exposure:
  22. include: '*'
  23. server:
  24. port: 7001

 服务端配置文件

  1. server:
  2. port: 7000
  3. spring:
  4. application:
  5. name: admin-server
  6. security:
  7. user:
  8. name: admin
  9. password: admin
  10. management:
  11. endpoint:
  12. health:
  13. show-details: always
  1. package com.base.admin.server.config;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  7. @Configuration
  8. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  9. private final String adminContextPath;
  10. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  11. this.adminContextPath = adminServerProperties.getContextPath();
  12. }
  13. @Override
  14. protected void configure(HttpSecurity http) throws Exception {
  15. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  16. successHandler.setTargetUrlParameter("redirectTo");
  17. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  18. http.authorizeRequests()
  19. //授予对所有静态资产和登录页面的公共访问权限
  20. .antMatchers(adminContextPath + "/assets/**").permitAll()
  21. .antMatchers(adminContextPath + "/login").permitAll()
  22. .antMatchers("/actuator/**").permitAll()
  23. //必须对每个其他请求进行身份验证
  24. .anyRequest().authenticated()
  25. .and()
  26. //配置登录和注销
  27. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  28. .logout().logoutUrl(adminContextPath + "/logout").and()
  29. //启用HTTP-Basic支持。这是Spring Boot Admin Client注册所必需的
  30. .httpBasic().and();
  31. }
  32. }

问题出现在权限控制这里,废话:

  1. package com.base.admin.server.config;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  7. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  8. @Configuration
  9. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  10. private final String adminContextPath;
  11. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  12. this.adminContextPath = adminServerProperties.getContextPath();
  13. }
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  17. successHandler.setTargetUrlParameter("redirectTo");
  18. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  19. http.authorizeRequests()
  20. .antMatchers("/assets/**").permitAll()
  21. .antMatchers("/login").permitAll()
  22. .anyRequest().authenticated().and()
  23. .formLogin().loginPage("/login")
  24. .successHandler(successHandler).and()
  25. .logout().logoutUrl("/logout").and()
  26. .httpBasic()
  27. .and()
  28. .csrf()
  29. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  30. //解决 401 问题
  31. .ignoringAntMatchers(
  32. "/instances",
  33. "/actuator/**"
  34. );
  35. }
  36. }

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

闽ICP备14008679号