spring cloud oauth2 登录一定次数后锁定

//定义用户登录失败事件
public class UserLoginFailedEvent extends ApplicationEvent {
    public UserLoginFailedEvent(Authentication authentication) {
        super(authentication);
    }
}

//用户登录失败监听器
@Slf4j
@Component
public class UserLoginFailedListener implements ApplicationListener<UserLoginFailedEvent> {
    //错误了第四次返回true,然后锁定账号,第五次即使密码正确也会报账户锁定
    Set<RequestLimitRule> rules = Collections.singleton(RequestLimitRule.of(1, TimeUnit.MINUTES, 3)); // 3 request per 1 minute, per key
    RequestRateLimiter limiter = new InMemorySlidingWindowRequestRateLimiter(rules);
    @Override
    public void onApplicationEvent(UserLoginFailedEvent event) {
        if (event.getSource() instanceof Authentication) {
            Authentication authentication = (Authentication) event.getSource();
            log.info(JSONObject.toJSONString(authentication));

            boolean reachLimit = limiter.overLimitWhenIncremented(authentication.getName());
            if (reachLimit) {
                throw new LockedException("账户被锁定");
            }
        }

        System.out.println("----用户验证信息---faile----------------------");

    }
}

//用户自定义身份认证
@Component
public class UserAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ApplicationEventPublisher publisher;


    /**
     * @Description 认证处理,返回一个Authentication的实现类则代表认证成功,返回null则代表认证失败
     * @Date 2019/7/5 15:19
     * @Version  1.0
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getName();
        String password = (String) authentication.getCredentials();
        if(StringUtils.isBlank(username)){
            throw new OAuth2Exception("username用户名不可以为空");
        }
        if(StringUtils.isBlank(password)){
            throw new OAuth2Exception("密码不可以为空");
        }
        //获取用户信息
        UserDetails user = myUserDetailsService.loadUserByUsername(username);
        //比较前端传入的密码明文和数据库中加密的密码是否相等
        if (!passwordEncoder.matches(password, user.getPassword())) {
            //发布密码不正确事件
            publisher.publishEvent(new UserLoginFailedEvent(authentication));
            throw new OAuth2Exception("用户名或者密码错误");
        }
        //获取用户权限信息
        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
        return new UsernamePasswordAuthenticationToken(user, password, authorities);

    }
    /**
     * @Description 如果该AuthenticationProvider支持传入的Authentication对象,则返回true
     * @Date 2019/7/5 15:18
     * @Version  1.0
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.equals(UsernamePasswordAuthenticationToken.class);
    }

}


//限流依赖
   <dependency>
            <groupId>es.moki.ratelimitj</groupId>
            <artifactId>ratelimitj-inmemory</artifactId>
            <version>0.6.0</version>
        </dependency>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud OAuth2是一个基于OAuth2实现的身份认证和授权框架,可以用于实现单点登录(SSO)功能。 单点登录是指在一个系统中登录后,可以在其他关联系统中自动登录,无需再次输入账号和密码。使用Spring Cloud OAuth2可以实现这样的功能。 首先,需要在认证服务器上使用Spring Security和Spring Cloud OAuth2的组件搭建一个OAuth2认证服务。该服务会负责用户的认证和授权工作。 在各个子系统中,需要引入Spring Cloud OAuth2的客户端,然后配置认证服务器的地址和客户端的凭证信息(clientId和clientSecret)。 当用户访问某个子系统时,子系统会重定向到认证服务器进行认证。用户在认证服务器上输入账号和密码进行认证,认证成功后,认证服务器会返回一个授权码给子系统。 子系统将授权码发送给认证服务器,认证服务器通过校验授权码的有效性,并且根据授权码发放一个访问令牌。子系统使用访问令牌进行后续的接口访问。 当用户在其他关联系统中访问时,这些系统会共享认证服务器上的会话信息,无需再次进行登录认证,直接使用之前的访问令牌进行接口访问。 通过以上步骤,就实现了Spring Cloud OAuth2的单点登录功能。用户只需要在一个系统登录一次,就可以在其他系统中自动登录,提高了用户体验。同时,认证服务器集中管理用户的认证和授权信息,提供了一种便捷的集中式身份管理方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值