微服务监控中心springboot-admin 配置登录密码

6130阅读 0评论2022-06-10 fhadmin
分类:Java


点击(此处)折叠或打开


  1. 1. pom 加入 security

  2.         <!-- 加入密码认证 -->
  3.         <dependency>
  4.             <groupId>org.springframework.boot</groupId>
  5.             <artifactId>spring-boot-starter-security</artifactId>
  6.         </dependency>
  7. 2. 加入配置类 SecuritySecureConfig

  8. package org.fh.config;
  9.  
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  13. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  14.  
  15. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  16.  
  17. /**
  18.  * 说明:SecuritySecure配置
  19.  * 作者:FH Admin
  20.  * from:fhadmin.cn
  21.  */
  22. @Configuration
  23. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  24.  
  25.     private final String adminContextPath;
  26.  
  27.     public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  28.         this.adminContextPath = adminServerProperties.getContextPath();
  29.     }
  30.  
  31.     @Override
  32.     protected void configure(HttpSecurity http) throws Exception {
  33.  
  34.         SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  35.         successHandler.setTargetUrlParameter("redirectTo");
  36.  
  37.         http.headers().frameOptions().disable();
  38.         
  39.         http.authorizeRequests().antMatchers(adminContextPath + "/assets/**",adminContextPath + "/actuator/**").permitAll()
  40.                 .antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin()
  41.                 .loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
  42.                 .logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf().disable();
  43.  
  44.     }
  45.     
  46. }

  47. 3.  配置 application.properties

  48. #开启安全认证 用户名和密码
  49. spring.security.user.name=fhadmin
  50. spring.security.user.password=root
  51. spring.security.basic.enabled=true



上一篇:spring boot html+vue.js 形式前后分离代码示例
下一篇:微服务监控中心springboot-admin 配置登录密码