社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
@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();
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!