成都创新互联网站制作重庆分公司

springSecurity中怎么自定义用户认证

spring Security中怎么自定义用户认证,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司服务项目包括始兴网站建设、始兴网站制作、始兴网页制作以及始兴网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,始兴网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到始兴省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询


  
  
  
  
  

  
  
    
    

    
    

    
    

    
    
      
    

    
    
  

  
  
    
    
      
    
  

  
    
  


  

配置完自定义的文件以后,在需要自定义认证类的模块中实现

UserDetailsService

package com.qingmu2.core.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.qingmu2.core.pojo.seller.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * 自定义的认证类
 * @Auther:qingmu
 * @Description:脚踏实地,只为出人头地
 * @Date:Created in 8:33 2019/5/31
 */
public class UserDetailServiceImpl implements UserDetailsService {

  private SellerService sellerService;

  public UserDetailServiceImpl() {
  }
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Seller seller = sellerService.findOne(username);
    if(null!=seller){
      //判断一次商家是否被审核通过.
      if("1".equals(seller.getStatus())){
        //创建一个集合,用来存储权限
        HashSet authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        //将这个用户的信息返回给认证类
        return new User(username,seller.getPassword(),authorities);
      }
    }
    //没有这个用户,则返回null
    return null;
  }

  public UserDetailServiceImpl(SellerService sellerService) {
    this.sellerService = sellerService;
  }

  public SellerService getSellerService() {
    return sellerService;
  }

  public void setSellerService(SellerService sellerService) {
    this.sellerService = sellerService;
  }
}

看完上述内容,你们掌握spring Security中怎么自定义用户认证的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


文章题目:springSecurity中怎么自定义用户认证
URL链接:http://cxhlcq.com/article/ipdgoh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部