/** * Processes an Authentication request. */ publicinterfaceAuthenticationManager{ /** * Attempts to authenticate the passed Authentication object, returning a fully populated Authentication object (including granted authorities) if successful. */ Authentication authenticate(Authentication authentication)throws AuthenticationException; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Indicates a class can process a specific Authentication implementation */ publicinterfaceAuthenticationProvider{ /** * Performs authentication with the same contract as AuthenticationManager.authenticate(Authentication). */ Authentication authenticate(Authentication authentication)throws AuthenticationException;
/** * Returns true if this AuthenticationProvider supports the indicated Authentication object. */ booleansupports(Class<?> authentication); }
/** * @author Joshua.H.Brooks * @description * @date 2022-07-05 23:31 */ publicclassMyAuthenticationProviderextendsDaoAuthenticationProvider{ /** 下面是抽象类 AbstractUserDetailsAuthenticationProvider#additionalAuthenticationChecks()方法的注释。 * Allows subclasses to perform any additional checks of a returned (or cached) UserDetails for a given authentication request. * Generally a subclass will at least compare the Authentication.getCredentials() with a UserDetails.getPassword(). * If custom logic is needed to compare additional properties of UserDetails and/or UsernamePasswordAuthenticationToken, * these should also appear in this method. * @param userDetails * @param authentication * @throws AuthenticationException */ @Override protectedvoidadditionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication)throws AuthenticationException { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); //用户前端输入的 String code = request.getParameter("code"); //后台生成的 String verifyCode = (String) request.getSession().getAttribute("verifyCode"); if(verifyCode == null || code == null || !code.equals(verifyCode)){ thrownew AuthenticationServiceException("验证么输入错误, 请注意大小写哦~"); } super.additionalAuthenticationChecks(userDetails, authentication); } }
/** * Represents the token for an authentication request or for an authenticated principal once the request has been processed by the AuthenticationManager.authenticate(Authentication) method. */ publicinterfaceAuthenticationextendsPrincipal, Serializable{ /** * Set by an AuthenticationManager to indicate the authorities that the principal has been granted. Note that classes should not rely on this value as being valid unless it has been set by a trusted AuthenticationManager. */ Collection<? extends GrantedAuthority> getAuthorities();
/** * The credentials that prove the principal is correct. This is usually a password, but could be anything relevant to the AuthenticationManager. Callers are expected to populate the credentials. */ Object getCredentials();
/** * Stores additional details about the authentication request. These might be an IP address, certificate serial number etc. */ Object getDetails();
/** * The identity of the principal being authenticated. In the case of an authentication request with username and password, this would be the username. Callers are expected to populate the principal for an authentication request. */ Object getPrincipal();