使用Spring Security给Spring Boot Admin做一个安全验证登录

项目中我们可用到Spring Boot Admin 应用监控 监控服务器的各项指标状态。

本类别文章已经介绍了 如何搭建Spring Boot Admin 截图正常运行效果图如下:






下边我们贴下关键实现该功能的过程

<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server-ui-login</artifactId>
   <version>${spring-boot-admin.version}</version>
</dependency>
<!--  spring-boot-starter-security -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
   <version>1.4.5.RELEASE</version>
</dependency>

关闭 原有的Basic认证


management:
  security:
    enabled: false



security:
  user:
    name: miyaow
    password: 123

  basic:
    enabled: false

定义重写我们的权限控制类

/**
 * 配置HTTPBASIC权限验证
 *
 * @author yesh
 *         (M.M)!
 *         Created by 2017/5/15.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        //忽略css.jq.img等文件
        web.ignoring().antMatchers("/**.html","/**.css", "/img/**", "/**.js","/third-party/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable() //HTTP with Disable CSRF
                .authorizeRequests() //Authorize Request Configuration
                .antMatchers( "/login",
                        "/api/**",
                        "/**/heapdump",
                        "/**/loggers",
                        "/**/liquibase",
                        "/**/logfile",
                        "/**/flyway",
                        "/**/auditevents",
                        "/**/jolokia").permitAll() //放开"/api/**":为了给被监控端免登录注册并解决Log与Logger冲突
                .and()
                .authorizeRequests()
                .antMatchers("/**").hasRole("USER")
                .antMatchers("/**").authenticated()
                .and() //Login Form configuration for all others
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login").permitAll()
                .defaultSuccessUrl("/")
                .and() //Logout Form configuration
                .logout()
                .deleteCookies("remove")
                .logoutSuccessUrl("/login.html").permitAll()
                .and()
                .httpBasic();

    }

并在启动类中添加开启功能注解

@Configuration
@EnableAdminServer //开启Spring Boot Admin 服务
@EnableDiscoveryClient
@SpringBootApplication
public class MiSpringBootAdminApplication {

   public static void main(String[] args) {
      SpringApplication.run(MiSpringBootAdminApplication.class, args);
   }

}


这样就基本上完成了基本的配置。

欢迎大家多给给意见我的开源项目,更多详情见我的MI系统介绍githun地址如下:


https://github.com/miyaow/mi













  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值