Spring Security源码学习

 

 

1、PasswordEncoder和PlaintextPasswordEncoder

密码校验

 

@Deprecated
public interface PasswordEncoder {

	String encodePassword(String rawPass, Object salt);

	boolean isPasswordValid(String encPass, String rawPass, Object salt);
}

 

 

package org.springframework.security.authentication.encoding;

import java.util.Locale;

/**
 * <p>
 * Plaintext implementation of PasswordEncoder.
 * </p>
 * <P>
 * As callers may wish to extract the password and salts separately from the encoded
 * password, the salt must not contain reserved characters (specifically '{' and '}').
 * </p>
 *
 * @author colin sampaleanu
 * @author Ben Alex
 */
public class PlaintextPasswordEncoder extends BasePasswordEncoder {
	// ~ Instance fields
	// ================================================================================================

	private boolean ignorePasswordCase = false;

	// ~ Methods
	// ========================================================================================================

	public String encodePassword(String rawPass, Object salt) {
		return mergePasswordAndSalt(rawPass, salt, true);
	}

	public boolean isIgnorePasswordCase() {
		return ignorePasswordCase;
	}

	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		String pass1 = encPass + "";

		// Strict delimiters is false because pass2 never persisted anywhere
		// and we want to avoid unnecessary exceptions as a result (the
		// authentication will fail as the encodePassword never allows them)
		String pass2 = mergePasswordAndSalt(rawPass, salt, false);

		if (ignorePasswordCase) {
			// Note: per String javadoc to get correct results for Locale insensitive, use
			// English
			pass1 = pass1.toLowerCase(Locale.ENGLISH);
			pass2 = pass2.toLowerCase(Locale.ENGLISH);
		}
		return PasswordEncoderUtils.equals(pass1, pass2);
	}

	/**
	 * Demerges the previously {@link #encodePassword(String, Object)}<code>String</code>.
	 * <P>
	 * The resulting array is guaranteed to always contain two elements. The first is the
	 * password, and the second is the salt.
	 * </p>
	 * <P>
	 * Throws an exception if <code>null</code> or an empty <code>String</code> is passed
	 * to the method.
	 * </p>
	 *
	 * @param password from {@link #encodePassword(String, Object)}
	 *
	 * @return an array containing the password and salt
	 */
	public String[] obtainPasswordAndSalt(String password) {
		return demergePasswordAndSalt(password);
	}

	/**
	 * Indicates whether the password comparison is case sensitive.
	 * <P>
	 * Defaults to <code>false</code>, meaning an exact case match is required.
	 * </p>
	 *
	 * @param ignorePasswordCase set to <code>true</code> for less stringent comparison
	 */
	public void setIgnorePasswordCase(boolean ignorePasswordCase) {
		this.ignorePasswordCase = ignorePasswordCase;
	}
}

密码校验


package org.springframework.security.authentication.encoding;

public class MessageDigestPasswordEncoder extends BaseDigestPasswordEncoder {

	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		String pass1 = "" + encPass;
		String pass2 = encodePassword(rawPass, salt);

		return PasswordEncoderUtils.equals(pass1, pass2);
	}

}

 

2、登录成功处理器AuthenticationAppSuccessHandler

 

@Component
public class AuthenticationAppSuccessHandler implements org.springframework.security.web.authentication.AuthenticationSuccessHandler {

@Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
      

            
        response.getWriter().write(JSON.toJSONString(success));
    }

 

3、

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值