Spring Security ,Spring Actuator添加登录认证


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

一、继承WebSecurityConfigurerAdapter

@Configuration(proxyBeanMethods = false)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

}

 

二、 配置限制

1. 不认证

@Override

protected void configure(HttpSecurity http) throws Exception {        
        /**
         * 所有都能訪問,放棄判斷權限
         */
       http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().permitAll());   

    }

2. 认证角色

@Override

protected void configure(HttpSecurity http) throws Exception {  

        /**
         * 只有角色ENDPOINT_ADMIN可以看
         */
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
        http.httpBasic();
        
    }

设置后,访问actuator,弹出登录框

三、配置用户名密码角色

1. 内存模式:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		PasswordEncoder pe = new PasswordEncoder(){

			@Override
			public String encode(CharSequence rawPassword) {
				System.out.println("rawPassword:"+rawPassword.toString());
				return null;
			}

			@Override
			public boolean matches(CharSequence rawPassword, String encodedPassword) {
				System.out.println("rawPassword2:"+rawPassword);
				System.out.println("encodedPassword2:"+encodedPassword);
				if(rawPassword.toString().equals(encodedPassword)){
					return true;
				}
				return false;
			}
			
		};
        auth
            .inMemoryAuthentication().passwordEncoder(pe)
            .withUser("admin").password("admin").roles("ENDPOINT_ADMIN");
    } 

2. 数据库模式

        auth.userDetailsService(dbUserDetailsService);

 

四、其他配置限制(未验证)

 

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/resources/**", "/signup", "/about").permitAll()
                .antMatchers("/duno/**").hasRole("ENDPOINT_ADMIN")
                .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .failureForwardUrl("/login?error")
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/index")
                .permitAll()
                .and()
            .httpBasic()
                .disable();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值