Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603729
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-06-10 10:30:18


点击(此处)折叠或打开


  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



阅读(5994) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~