Simplify hellowebflux

This sample should be absolute minimal example.
This commit is contained in:
Rob Winch
2017-09-13 09:54:32 -05:00
parent 7bb4367cf1
commit 164404c7d3
6 changed files with 141 additions and 434 deletions

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
/**
* @author Rob Winch
* @since 5.0
*/
@RestController
public class HelloUserController {
@GetMapping("/")
public Mono<Map<String,String>> hello(Mono<Principal> principal) {
return principal
.map(Principal::getName)
.map(this::helloMessage);
}
private Map<String,String> helloMessage(String username) {
return Collections.singletonMap("message", "Hello " + username + "!");
}
}

View File

@@ -0,0 +1,42 @@
/*
*
* * Copyright 2002-2017 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package sample;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.core.userdetails.MapUserDetailsRepository;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
/**
* @author Rob Winch
* @since 5.0
*/
@EnableWebFluxSecurity
public class HelloWebfluxSecurityConfig {
@Bean
public MapUserDetailsRepository userDetailsRepository() {
UserDetails user = User.withUsername("user")
.password("user")
.roles("USER")
.build();
return new MapUserDetailsRepository(user);
}
}

View File

@@ -1,64 +0,0 @@
/*
*
* * Copyright 2002-2017 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package sample;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.MapUserDetailsRepository;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authorization.AuthorizationContext;
import reactor.core.publisher.Mono;
/**
* @author Rob Winch
* @since 5.0
*/
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
SecurityWebFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeExchange()
.pathMatchers("/admin/**").hasRole("ADMIN")
.pathMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
.anyExchange().authenticated()
.and()
.build();
}
private Mono<AuthorizationDecision> currentUserMatchesPath(Mono<Authentication> authentication, AuthorizationContext context) {
return authentication
.map( a -> context.getVariables().get("user").equals(a.getName()))
.map( granted -> new AuthorizationDecision(granted));
}
@Bean
public MapUserDetailsRepository userDetailsRepository() {
UserDetails rob = User.withUsername("rob").password("rob").roles("USER").build();
UserDetails admin = User.withUsername("admin").password("admin").roles("USER","ADMIN").build();
return new MapUserDetailsRepository(rob, admin);
}
}

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
/**
* @author Rob Winch
* @since 5.0
*/
@RestController
public class UserController {
@GetMapping("/me")
public Mono<Map<String,String>> me(@AuthenticationPrincipal UserDetails user) {
return me(Mono.just(user));
}
@GetMapping("/mono/me")
public Mono<Map<String,String>> me(@AuthenticationPrincipal Mono<UserDetails> user) {
return user.flatMap( u -> Mono.just(Collections.singletonMap("username", u.getUsername())));
}
@GetMapping("/mono/session")
public Mono<Map<String,Object>> Session(Mono<WebSession> session) {
return session.flatMap( s -> Mono.just(s.getAttributes()));
}
@GetMapping("/principal")
public Mono<Map<String,String>> principal(Principal principal) {
return principal(Mono.just(principal));
}
@GetMapping("/mono/principal")
public Mono<Map<String,String>> principal(Mono<Principal> principal) {
return principal.flatMap( p -> Mono.just(Collections.singletonMap("username", p.getName())));
}
@GetMapping("/admin")
public Map<String,String> admin() {
return Collections.singletonMap("isadmin", "true");
}
}