我创建了一个自定义AuthenticationProvider来执行自定义安全检查.我还创建了继承的自定义异常,AccountStatusException以通知用户状态问题,例如用户在特定时间段内未验证其帐户的UserDetails情况.我也是自定义实现.
这是我执行的安全检查的代码.省略了与案例无关的代码.
public class SsoAuthenticationProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = (String) authentication.getPrincipal();
User user = null;
if (username != null) {
user = getUserRepository().findByUserName(username);
if (user != null) {
if (user.getEnabled() != 0) {
if ((user.getUserDetail().getConfirmed() != 0)
|| ((new Date().getTime() - user.getUserDetail().getRequestDate().getTime()) / (1000 * 60 * 60 * 24)) <= getUnconfirmedDays()) {
if (getPasswordEncoder().isPasswordValid(user.getPassword(),
(String) authentication.getCredentials(), user)) {
user.authenticated = true;
user.getAuthorities();
}
} …Run Code Online (Sandbox Code Playgroud)