@startuml Title "Authentication类图" interface Principal interface Authentication interface AuthenticationManager interface AuthenticationProvider abstract class AbstractUserDetailsAuthenticationProvider class ProviderManager class DaoAuthenticationProvider interface UserDetailsService
@startuml Title "Authentication类图" interface Principal interface Authentication interface AuthenticationManager interface AuthenticationProvider abstract class AbstractUserDetailsAuthenticationProvider class ProviderManager class DaoAuthenticationProvider interface UserDetailsService
/** * 对原始密码进行编码。通常,一个好的编码算法应用SHA-1或更大的哈希值和一个8字节或更大的随机生成的salt。 * Encode the raw password. Generally, a good encoding algorithm applies a SHA-1 or greater hash combined with an 8-byte or greater randomly generated salt. * * @param rawPassword * @return */ @Override public String encode(CharSequence rawPassword) { String salt; if (this.strength > 0) { if (this.random != null) { salt = BCrypt.gensalt(this.strength, this.random); } else { salt = BCrypt.gensalt(this.strength); } } else { salt = BCrypt.gensalt(); }
/** * 验证从存储中获得的已编码密码在经过编码后是否与提交的原始密码匹配。 * 如果密码匹配,返回true;如果密码不匹配,返回false。存储的密码本身永远不会被解码。 * * @param rawPassword the raw password to encode and match * @param encodedPassword the encoded password from storage to compare with * @return */ @Override publicbooleanmatches(CharSequence rawPassword, String encodedPassword) { if (encodedPassword != null && encodedPassword.length() != 0) { if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) { this.logger.warn("Encoded password does not look like BCrypt"); returnfalse; } else { return BCrypt.checkpw(rawPassword.toString(), encodedPassword); } } else { this.logger.warn("Empty encoded password"); returnfalse; } }
/** * 如果为了更好的安全性,应该再次对已编码的密码进行编码,则返回true,否则为false。 * * @param encodedPassword the encoded password to check * @return Returns true if the encoded password should be encoded again for better security, else false. The default implementation always returns false. */ @Override publicbooleanupgradeEncoding(String encodedPassword) { returnfalse; } }
/** * 返回用户所有角色的封装,一个Role对应一个GrantedAuthority * * @return 返回授予用户的权限。 */ @Override public Collection<? extendsGrantedAuthority> getAuthorities() { /* Collection<GrantedAuthority> authorities = new ArrayList<>(); String username = this.getUsername(); if (username != null) { SimpleGrantedAuthority authority = new SimpleGrantedAuthority(username); authorities.add(authority); }*/ return authorities; }
/** * 返回用于验证用户身份的密码。 * * @return Returns the password used to authenticate the user. */ @Override public String getPassword() { return appUser.getPassword(); }
/** * 对原始密码进行编码。通常,一个好的编码算法应用SHA-1或更大的哈希值和一个8字节或更大的随机生成的salt。 * Encode the raw password. Generally, a good encoding algorithm applies a SHA-1 or greater hash combined with an 8-byte or greater randomly generated salt. * * @param rawPassword * @return */ @Override public String encode(CharSequence rawPassword) { String salt; if (this.strength > 0) { if (this.random != null) { salt = BCrypt.gensalt(this.strength, this.random); } else { salt = BCrypt.gensalt(this.strength); } } else { salt = BCrypt.gensalt(); }
/** * 验证从存储中获得的已编码密码在经过编码后是否与提交的原始密码匹配。 * 如果密码匹配,返回true;如果密码不匹配,返回false。存储的密码本身永远不会被解码。 * * @param rawPassword the raw password to encode and match * @param encodedPassword the encoded password from storage to compare with * @return */ @Override publicbooleanmatches(CharSequence rawPassword, String encodedPassword) { if (encodedPassword != null && encodedPassword.length() != 0) { if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) { this.logger.warn("Encoded password does not look like BCrypt"); returnfalse; } else { return BCrypt.checkpw(rawPassword.toString(), encodedPassword); } } else { this.logger.warn("Empty encoded password"); returnfalse; } }
/** * 如果为了更好的安全性,应该再次对已编码的密码进行编码,则返回true,否则为false。 * * @param encodedPassword the encoded password to check * @return Returns true if the encoded password should be encoded again for better security, else false. The default implementation always returns false. */ @Override publicbooleanupgradeEncoding(String encodedPassword) { returnfalse; } }
if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) { log.debug("Authentication failed: password does not match stored value");
/** * <p>403响应</p> * JwtAuthenticationEntryPoint * * @author maxzhao * @date 2019-07-04 18:24 */ publicclassJwtAuthenticationEntryPointimplementsAuthenticationEntryPoint { /** * Commences an authentication scheme. * 启动身份验证方案。. * <p>填充 populate * <code>ExceptionTranslationFilter</code> will populate the <code>HttpSession</code> * attribute named * <code>AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY</code> * with the requested target URL before calling this method. * <p> * Implementations should modify the headers on the <code>ServletResponse</code> as * necessary to commence the authentication process. * * @param request that resulted in an <code>AuthenticationException</code> * @param response so that the user agent can begin authentication * @param authException that caused the invocation */ @Override publicvoidcommence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)throws IOException, ServletException { // 么有权限 // Full authentication is required to access this resource // response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); response.setStatus(HttpServletResponse.SC_FORBIDDEN); // String reason = "统一处理,原因:" + authException.getMessage(); response.getWriter().write(ResultObj.getErrorResponse("", "统一处理,原因:" + authException.getMessage()).toJSON()); // response.getWriter().write(new ObjectMapper().writeValueAsString(reason)); } }
tar spring-boot-cli-2.2.0.BUILD-20190222.193142-143-bin.tar.gz -C /home/maxzhao/ cd /home/maxzhao mv spring-2.2.0.BUILD-SNAPSHOT spring-2.2.0-cli cd spring-2.2.0-cli