WebUserAuthenticationToken.java
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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 org.gringlobal.custom.security;
import java.util.Collection;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import lombok.EqualsAndHashCode;
import javax.security.auth.Subject;
/**
* Custom implementation of {@link AbstractAuthenticationToken}.
*
* We require a logic of {@link UsernamePasswordAuthenticationToken} but we cannot extend this class
* because only {@link WebUserAuthenticationProvider} must be able to handle it. Otherwise,
* {@link DaoAuthenticationProvider} will try to authenticate this token as well when {@link WebUserAuthenticationProvider}
* throws {@link UsernameNotFoundException}.
*
* @author Maxym Borodenko
*/
@EqualsAndHashCode
public class WebUserAuthenticationToken implements Authentication, CredentialsContainer {
private static final long serialVersionUID = 3551184593105312881L;
private final UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken;
public WebUserAuthenticationToken(Object principal, Object credentials) {
usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(principal, credentials);
}
public WebUserAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
}
@Override
public Object getCredentials() {
return usernamePasswordAuthenticationToken.getCredentials();
}
@Override
public Object getPrincipal() {
return usernamePasswordAuthenticationToken.getPrincipal();
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
usernamePasswordAuthenticationToken.setAuthenticated(isAuthenticated);
}
@Override
public void eraseCredentials() {
usernamePasswordAuthenticationToken.eraseCredentials();
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
return usernamePasswordAuthenticationToken.getAuthorities();
}
@Override
public String getName() {
return usernamePasswordAuthenticationToken.getName();
}
@Override
public boolean isAuthenticated() {
return usernamePasswordAuthenticationToken.isAuthenticated();
}
@Override
public Object getDetails() {
return usernamePasswordAuthenticationToken.getDetails();
}
public void setDetails(Object details) {
usernamePasswordAuthenticationToken.setDetails(details);
}
@Override
public String toString() {
return usernamePasswordAuthenticationToken.toString();
}
@Override
public boolean implies(Subject subject) {
return usernamePasswordAuthenticationToken.implies(subject);
}
}