• 大小: 5KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-17
  • 语言: 其他
  • 标签: security  

资源简介

SpringBoot之整合Spring Security,SpringBoot之整合Spring SecuritySpringBoot之整合Spring SecuritySpringBoot之整合Spring Security

资源截图

代码片段和文件信息

package com.yangle.security;

import com.yangle.util.GuavaDataCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * Created by yangle on 2017/10/9.
 */
@Component
public class CustAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private SnailUserDetailsService userService;
    @Autowired
    private GuavaDataCache guavaDataCache;
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getName();
        String password = (String) authentication.getCredentials();
        CustUserDetails userDetials = (CustUserDetails) userService.loadUserByUsername(username);
        CustomWebAuthenticationDetails details = (CustomWebAuthenticationDetails) authentication.getDetails();
        String code=details.getCode();
        String kapacode=details.getKapacode();

        String value=guavaDataCache.getPropertyValue(“randcode““on“);
        if(value.equals(“on“)){
        if(!kapacode.equals(code)){
            throw new BadCredentialsException(“验证码不正确“);
        }
        }

        if(userDetials == null){
            throw new BadCredentialsException(“没有这个用户.“);
        }

        //加密过程在这里体现
        if (!password.equals(userDetials.getPassword())) {
            throw new BadCredentialsException(“密码错误“);
        }

        Collection authorities = userDetials.getAuthorities();
        return new UsernamePasswordAuthenticationToken(userDetials password authorities);
    }
    @Override
    public boolean supports(Class arg0) {
        return true;
    }

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-08 16:17  security\
     文件        2265  2018-01-11 15:04  security\CustAuthenticationProvider.java
     文件         688  2018-05-08 15:11  security\CustomAuthenticationDetailsSource.java
     文件        1123  2018-05-08 15:11  security\CustomWebAuthenticationDetails.java
     文件        2033  2018-01-03 20:46  security\CustUserDetails.java
     文件        1015  2018-01-11 10:44  security\SnailUserDetailsService.java
     文件        2712  2018-01-13 15:57  security\WebSecurityConfig.java

评论

共有 条评论