|
|
|
@ -16,6 +16,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
package smoketest.actuator.customsecurity; |
|
|
|
package smoketest.actuator.customsecurity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; |
|
|
|
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; |
|
|
|
import org.springframework.boot.actuate.web.mappings.MappingsEndpoint; |
|
|
|
import org.springframework.boot.actuate.web.mappings.MappingsEndpoint; |
|
|
|
import org.springframework.boot.autoconfigure.security.servlet.PathRequest; |
|
|
|
import org.springframework.boot.autoconfigure.security.servlet.PathRequest; |
|
|
|
@ -25,36 +28,44 @@ import org.springframework.security.config.Customizer; |
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
|
|
import org.springframework.security.core.userdetails.User; |
|
|
|
import org.springframework.security.core.userdetails.User; |
|
|
|
|
|
|
|
import org.springframework.security.core.userdetails.User.UserBuilder; |
|
|
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails; |
|
|
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
|
|
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false) |
|
|
|
@Configuration(proxyBeanMethods = false) |
|
|
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { |
|
|
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
public InMemoryUserDetailsManager inMemoryUserDetailsManager() { |
|
|
|
public InMemoryUserDetailsManager inMemoryUserDetailsManager() { |
|
|
|
return new InMemoryUserDetailsManager( |
|
|
|
List<UserDetails> userDetails = new ArrayList<>(); |
|
|
|
User.withDefaultPasswordEncoder().username("user").password("password").authorities("ROLE_USER") |
|
|
|
userDetails.add(createUserDetails("user", "password", "ROLE_USER")); |
|
|
|
.build(), |
|
|
|
userDetails.add(createUserDetails("beans", "beans", "ROLE_BEANS")); |
|
|
|
User.withDefaultPasswordEncoder().username("beans").password("beans").authorities("ROLE_BEANS").build(), |
|
|
|
userDetails.add(createUserDetails("admin", "admin", "ROLE_ACTUATOR", "ROLE_USER")); |
|
|
|
User.withDefaultPasswordEncoder().username("admin").password("admin") |
|
|
|
return new InMemoryUserDetailsManager(userDetails); |
|
|
|
.authorities("ROLE_ACTUATOR", "ROLE_USER").build()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
|
|
|
private UserDetails createUserDetails(String username, String password, String... authorities) { |
|
|
|
|
|
|
|
UserBuilder builder = User.withDefaultPasswordEncoder(); |
|
|
|
|
|
|
|
builder.username(username); |
|
|
|
|
|
|
|
builder.password(password); |
|
|
|
|
|
|
|
builder.authorities(authorities); |
|
|
|
|
|
|
|
return builder.build(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
// @formatter:off
|
|
|
|
http.authorizeRequests((requests) -> { |
|
|
|
http.authorizeRequests((requests) -> |
|
|
|
requests.mvcMatchers("/actuator/beans").hasRole("BEANS"); |
|
|
|
requests |
|
|
|
requests.requestMatchers(EndpointRequest.to("health", "info")).permitAll(); |
|
|
|
.mvcMatchers("/actuator/beans").hasRole("BEANS") |
|
|
|
requests.requestMatchers(EndpointRequest.toAnyEndpoint().excluding(MappingsEndpoint.class)) |
|
|
|
.requestMatchers(EndpointRequest.to("health", "info")).permitAll() |
|
|
|
.hasRole("ACTUATOR"); |
|
|
|
.requestMatchers(EndpointRequest.toAnyEndpoint().excluding(MappingsEndpoint.class)).hasRole("ACTUATOR") |
|
|
|
requests.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll(); |
|
|
|
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() |
|
|
|
requests.antMatchers("/foo").permitAll(); |
|
|
|
.antMatchers("/foo").permitAll() |
|
|
|
requests.antMatchers("/**").hasRole("USER"); |
|
|
|
.antMatchers("/**").hasRole("USER")) |
|
|
|
}); |
|
|
|
.cors(Customizer.withDefaults()) |
|
|
|
http.cors(Customizer.withDefaults()); |
|
|
|
.httpBasic(); |
|
|
|
http.httpBasic(); |
|
|
|
// @formatter:on
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|