add security exc

This commit is contained in:
Loredana Crusoveanu 2018-07-25 22:53:14 +03:00
parent 9101461a0a
commit 8b5232c1b6
1 changed files with 23 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package com.baeldung.reactive.security; package com.baeldung.reactive.security;
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
@ -9,6 +10,8 @@ import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.server.SecurityWebFilterChain; import org.springframework.security.web.server.SecurityWebFilterChain;
import com.baeldung.reactive.actuator.FeaturesEndpoint;
@EnableWebFluxSecurity @EnableWebFluxSecurity
@EnableReactiveMethodSecurity @EnableReactiveMethodSecurity
public class SecurityConfig { public class SecurityConfig {
@ -16,25 +19,33 @@ public class SecurityConfig {
@Bean @Bean
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
return http.authorizeExchange() return http.authorizeExchange()
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN") .pathMatchers("/admin")
.anyExchange().permitAll() .hasAuthority("ROLE_ADMIN")
.and().formLogin() .matchers(EndpointRequest.to(FeaturesEndpoint.class))
.and().build(); .permitAll()
.anyExchange()
.permitAll()
.and()
.formLogin()
.and()
.csrf()
.disable()
.build();
} }
@Bean @Bean
public MapReactiveUserDetailsService userDetailsService() { public MapReactiveUserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder() UserDetails user = User.withDefaultPasswordEncoder()
.username("user") .username("user")
.password("password") .password("password")
.roles("USER") .roles("USER")
.build(); .build();
UserDetails admin = User.withDefaultPasswordEncoder() UserDetails admin = User.withDefaultPasswordEncoder()
.username("admin") .username("admin")
.password("password") .password("password")
.roles("ADMIN") .roles("ADMIN")
.build(); .build();
return new MapReactiveUserDetailsService(user, admin); return new MapReactiveUserDetailsService(user, admin);
} }