Polish core format

Issue gh-8945
This commit is contained in:
Rob Winch
2020-08-24 09:48:12 -05:00
parent 254f2e2aec
commit 4fd67b48e0
19 changed files with 150 additions and 39 deletions

View File

@@ -82,10 +82,13 @@ public class PrePostAdviceReactiveMethodInterceptor implements MethodInterceptor
Class<?> targetClass = invocation.getThis().getClass();
Collection<ConfigAttribute> attributes = this.attributeSource.getAttributes(method, targetClass);
PreInvocationAttribute preAttr = findPreInvocationAttribute(attributes);
// @formatter:off
Mono<Authentication> toInvoke = ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication).defaultIfEmpty(this.anonymous)
.map(SecurityContext::getAuthentication)
.defaultIfEmpty(this.anonymous)
.filter((auth) -> this.preInvocationAdvice.before(auth, invocation, preAttr))
.switchIfEmpty(Mono.defer(() -> Mono.error(new AccessDeniedException("Denied"))));
// @formatter:on
PostInvocationAttribute attr = findPostInvocationAttribute(attributes);
if (Mono.class.isAssignableFrom(returnType)) {
return toInvoke.flatMap((auth) -> PrePostAdviceReactiveMethodInterceptor.<Mono<?>>proceed(invocation)

View File

@@ -91,11 +91,16 @@ public abstract class AbstractUserDetailsReactiveAuthenticationManager implement
public Mono<Authentication> authenticate(Authentication authentication) {
String username = authentication.getName();
String presentedPassword = (String) authentication.getCredentials();
return retrieveUser(username).doOnNext(this.preAuthenticationChecks::check).publishOn(this.scheduler)
// @formatter:off
return retrieveUser(username)
.doOnNext(this.preAuthenticationChecks::check)
.publishOn(this.scheduler)
.filter((userDetails) -> this.passwordEncoder.matches(presentedPassword, userDetails.getPassword()))
.switchIfEmpty(Mono.defer(() -> Mono.error(new BadCredentialsException("Invalid Credentials"))))
.flatMap((userDetails) -> upgradeEncodingIfNecessary(userDetails, presentedPassword))
.doOnNext(this.postAuthenticationChecks::check).map(this::createUsernamePasswordAuthenticationToken);
.doOnNext(this.postAuthenticationChecks::check)
.map(this::createUsernamePasswordAuthenticationToken);
// @formatter:on
}
private Mono<UserDetails> upgradeEncodingIfNecessary(UserDetails userDetails, String presentedPassword) {

View File

@@ -48,7 +48,11 @@ public class DelegatingReactiveAuthenticationManager implements ReactiveAuthenti
@Override
public Mono<Authentication> authenticate(Authentication authentication) {
return Flux.fromIterable(this.delegates).concatMap((m) -> m.authenticate(authentication)).next();
// @formatter:off
return Flux.fromIterable(this.delegates)
.concatMap((m) -> m.authenticate(authentication))
.next();
// @formatter:on
}
}

View File

@@ -47,8 +47,12 @@ public class ReactiveAuthenticationManagerAdapter implements ReactiveAuthenticat
@Override
public Mono<Authentication> authenticate(Authentication token) {
return Mono.just(token).publishOn(this.scheduler).flatMap(this::doAuthenticate)
// @formatter:off
return Mono.just(token)
.publishOn(this.scheduler)
.flatMap(this::doAuthenticate)
.filter(Authentication::isAuthenticated);
// @formatter:on
}
private Mono<Authentication> doAuthenticate(Authentication authentication) {

View File

@@ -43,9 +43,14 @@ public class AuthorityReactiveAuthorizationManager<T> implements ReactiveAuthori
@Override
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) {
return authentication.filter((a) -> a.isAuthenticated()).flatMapIterable(Authentication::getAuthorities)
.map(GrantedAuthority::getAuthority).any(this.authorities::contains).map(AuthorizationDecision::new)
// @formatter:off
return authentication.filter((a) -> a.isAuthenticated())
.flatMapIterable(Authentication::getAuthorities)
.map(GrantedAuthority::getAuthority)
.any(this.authorities::contains)
.map(AuthorizationDecision::new)
.defaultIfEmpty(new AuthorizationDecision(false));
// @formatter:on
}
/**

View File

@@ -47,9 +47,12 @@ public interface ReactiveAuthorizationManager<T> {
* denied
*/
default Mono<Void> verify(Mono<Authentication> authentication, T object) {
return check(authentication, object).filter(AuthorizationDecision::isGranted)
// @formatter:off
return check(authentication, object)
.filter(AuthorizationDecision::isGranted)
.switchIfEmpty(Mono.defer(() -> Mono.error(new AccessDeniedException("Access Denied"))))
.flatMap((decision) -> Mono.empty());
// @formatter:on
}
}

View File

@@ -41,8 +41,11 @@ public final class ReactiveSecurityContextHolder {
* @return the {@code Mono<SecurityContext>}
*/
public static Mono<SecurityContext> getContext() {
return Mono.subscriberContext().filter(ReactiveSecurityContextHolder::hasSecurityContext)
// @formatter:off
return Mono.subscriberContext()
.filter(ReactiveSecurityContextHolder::hasSecurityContext)
.flatMap(ReactiveSecurityContextHolder::getSecurityContext);
// @formatter:on
}
private static boolean hasSecurityContext(Context context) {

View File

@@ -72,15 +72,22 @@ public class MapReactiveUserDetailsService implements ReactiveUserDetailsService
@Override
public Mono<UserDetails> updatePassword(UserDetails user, String newPassword) {
return Mono.just(user).map((userDetails) -> withNewPassword(userDetails, newPassword))
// @formatter:off
return Mono.just(user)
.map((userDetails) -> withNewPassword(userDetails, newPassword))
.doOnNext((userDetails) -> {
String key = getKey(user.getUsername());
this.users.put(key, userDetails);
});
// @formatter:on
}
private UserDetails withNewPassword(UserDetails userDetails, String newPassword) {
return User.withUserDetails(userDetails).password(newPassword).build();
// @formatter:off
return User.withUserDetails(userDetails)
.password(newPassword)
.build();
// @formatter:on
}
private String getKey(String username) {

View File

@@ -301,10 +301,15 @@ public class User implements UserDetails, CredentialsContainer {
}
public static UserBuilder withUserDetails(UserDetails userDetails) {
return withUsername(userDetails.getUsername()).password(userDetails.getPassword())
.accountExpired(!userDetails.isAccountNonExpired()).accountLocked(!userDetails.isAccountNonLocked())
.authorities(userDetails.getAuthorities()).credentialsExpired(!userDetails.isCredentialsNonExpired())
// @formatter:off
return withUsername(userDetails.getUsername())
.password(userDetails.getPassword())
.accountExpired(!userDetails.isAccountNonExpired())
.accountLocked(!userDetails.isAccountNonLocked())
.authorities(userDetails.getAuthorities())
.credentialsExpired(!userDetails.isCredentialsNonExpired())
.disabled(!userDetails.isEnabled());
// @formatter:on
}
private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {

View File

@@ -25,6 +25,7 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
@@ -109,15 +110,23 @@ import org.springframework.util.Assert;
*/
public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, MessageSourceAware {
public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled " + "from users "
// @formatter:off
public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users "
+ "where username = ?";
// @formatter:on
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority " + "from authorities "
// @formatter:off
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities "
+ "where username = ?";
// @formatter:on
// @formatter:off
public static final String DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERY = "select g.id, g.group_name, ga.authority "
+ "from groups g, group_members gm, group_authorities ga " + "where gm.username = ? "
+ "and g.id = ga.group_id " + "and g.id = gm.group_id";
+ "from groups g, group_members gm, group_authorities ga "
+ "where gm.username = ? " + "and g.id = ga.group_id " + "and g.id = gm.group_id";
// @formatter:on
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
@@ -199,12 +208,15 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
* objects. There should normally only be one matching user.
*/
protected List<UserDetails> loadUsersByUsername(String username) {
return getJdbcTemplate().query(this.usersByUsernameQuery, new String[] { username }, (rs, rowNum) -> {
// @formatter:off
RowMapper<UserDetails> mapper = (rs, rowNum) -> {
String username1 = rs.getString(1);
String password = rs.getString(2);
boolean enabled = rs.getBoolean(3);
return new User(username1, password, enabled, true, true, true, AuthorityUtils.NO_AUTHORITIES);
});
};
// @formatter:on
return getJdbcTemplate().query(this.usersByUsernameQuery, mapper, username);
}
/**

View File

@@ -173,7 +173,7 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
*/
@Override
protected List<UserDetails> loadUsersByUsername(String username) {
return getJdbcTemplate().query(getUsersByUsernameQuery(), new String[] { username }, this::mapToUser);
return getJdbcTemplate().query(getUsersByUsernameQuery(), this::mapToUser, username);
}
private UserDetails mapToUser(ResultSet rs, int rowNum) throws SQLException {