spring security 一个验证码登录例子

本文介绍了使用Spring Security实现验证码登录的过程。相较于Shiro,Spring Security的复杂性可能导致初学者困惑,但其过滤器链机制提供了灵活性。文章指出,Spring Security通过过滤器进行认证和授权,并可以自动配置或自定义过滤器。对于验证码或密码登录的实现,需要修改认证过滤器,例如扩展AbstractUserDetailsAuthenticationProvider并适配KdUsernamePasswordAuthenticationToken。
摘要由CSDN通过智能技术生成

看完shiro,在看spring security感觉快了很多,最开始看spring security的时候,非常晕,看完我觉得spring security做了太多事,以至于程序员都不知道,是怎么实现的,这样的

后果就是 当出现错误,或者需要修改的时候感觉无从下手。


个人理解,若有错误,请指正。

spring security跟shiro类似,都是使用过滤器来认证和授权,不同的是spring seciruty是实现了一个过滤器链,每个请求都要经过,我们可以使用自动配置,这样spring security自动帮我们配置了这一系列过滤器,也可以自定义过滤器放在它的过滤器链中。


验证码或密码登录,需要重新修改认证过滤器

package com.test.hello.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

public class KdUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
	 private boolean postOnly = true;
	 public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
	        if (this.postOnly && !request.getMethod().equals("POST")) {
	            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
	        }

	        String username = obtainUsername(request);
	        String password = obtainPassword(request);
	        String type = request.getParameter("j_type");

	        if (username == null) {
	            username = "";
	        }

	        if (password == null) {
	            password = "";
	        }
	        
	        if (type == null) {
	        	type = "1";
	        }

	        username = username.trim();

	        Authentication authRequest;
	        if(type.equals("1")){
	        	 authRequest = new UsernamePasswordAuthenticationToken(username, password);
	        }else{
	        	 authRequest = new KdUsernamePasswordAuthenticationToken(username, password,type);
	        }
	       
	        
	       

	        // Allow subclasses to set the "details" property
	        setDetails(request, (AbstractAuthenticationToken)authRequest);

	        return this.getAuthenticationManager().authenticate(authRequest);
	    }
	 
	    /**
	     * Provided so that subclasses may configure what is put into the authentication request's details
	     * property.
	     *
	     * @param request that an authentication request is being created for
	     * @param authRequest the authentication request object that should have its details set
	     */
	    protected void setDetails(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
	        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
	    }



}

type为2时候,使用验证码登录,token- >provider ->

token

package com.test.hello.security;

import java.util.Collection;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

public class KdUsernamePasswordAuthenticationToken extends AbstractAuthenticationToken{

	 //~ Instance fields ================================================================================================

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值