Simplify webflux samples
Remove the custom user from the hellowebflux and hellowebfluxfn samples.
This commit is contained in:
@@ -1,58 +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 java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
@Service
|
||||
public class MapUserRepository implements UserRepository {
|
||||
private final Map<String,User> users = new HashMap<>();
|
||||
|
||||
public MapUserRepository() {
|
||||
save(new User("rob", "rob", "Rob", "Winch")).block();
|
||||
save(new User("admin", "admin", "Admin", "User")).block();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<User> findAll() {
|
||||
return Flux.fromIterable(users.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<User> findByUsername(String username) {
|
||||
User result = users.get(username);
|
||||
|
||||
return result == null ? Mono.empty() : Mono.just(result);
|
||||
}
|
||||
|
||||
public Mono<User> save(User user) {
|
||||
users.put(user.getUsername(), user);
|
||||
return Mono.just(user);
|
||||
}
|
||||
}
|
||||
@@ -19,10 +19,13 @@
|
||||
package sample;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.authentication.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.authorization.AuthorizationContext;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -49,4 +52,12 @@ public class SecurityConfig {
|
||||
.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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,84 +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;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
|
||||
public User() {}
|
||||
|
||||
public User(User copy) {
|
||||
this(copy.getUsername(), copy.getPassword(), copy.getFirstname(), copy.getLastname());
|
||||
}
|
||||
|
||||
public User(String username, String password, String firstname, String lastname) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public void setFirstname(String firstname) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
|
||||
public void setLastname(String lastname) {
|
||||
this.lastname = lastname;
|
||||
}
|
||||
}
|
||||
@@ -16,37 +16,31 @@
|
||||
|
||||
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;
|
||||
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import org.springframework.web.server.WebSession;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
@RestController
|
||||
public class UserController {
|
||||
private final UserRepository users;
|
||||
|
||||
public UserController(UserRepository users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
public Mono<Map<String,String>> me(@AuthenticationPrincipal User user) {
|
||||
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<User> user) {
|
||||
public Mono<Map<String,String>> me(@AuthenticationPrincipal Mono<UserDetails> user) {
|
||||
return user.flatMap( u -> Mono.just(Collections.singletonMap("username", u.getUsername())));
|
||||
}
|
||||
|
||||
@@ -55,11 +49,6 @@ public class UserController {
|
||||
return session.flatMap( s -> Mono.just(s.getAttributes()));
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public Flux<User> users() {
|
||||
return this.users.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/principal")
|
||||
public Mono<Map<String,String>> principal(Principal principal) {
|
||||
return principal(Mono.just(principal));
|
||||
|
||||
@@ -1,33 +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 reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
public interface UserRepository {
|
||||
|
||||
Flux<User> findAll();
|
||||
|
||||
Mono<User> findByUsername(String username);
|
||||
|
||||
Mono<User> save(User user);
|
||||
}
|
||||
@@ -1,87 +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 java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.authentication.UserDetailsRepository;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.0
|
||||
*/
|
||||
@Component
|
||||
public class UserRepositoryUserDetailsRepository implements UserDetailsRepository {
|
||||
private final UserRepository users;
|
||||
|
||||
public UserRepositoryUserDetailsRepository(UserRepository users) {
|
||||
super();
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<UserDetails> findByUsername(String username) {
|
||||
return this.users
|
||||
.findByUsername(username)
|
||||
.map(UserDetailsAdapter::new);
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class UserDetailsAdapter extends User implements UserDetails {
|
||||
private static List<GrantedAuthority> USER_ROLES = AuthorityUtils.createAuthorityList("ROLE_USER");
|
||||
private static List<GrantedAuthority> ADMIN_ROLES = AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER");
|
||||
|
||||
private UserDetailsAdapter(User delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return isAdmin() ? ADMIN_ROLES : USER_ROLES ;
|
||||
}
|
||||
|
||||
private boolean isAdmin() {
|
||||
return getUsername().contains("admin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user