资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springSecurity如何自定义用户认证

这篇文章将为大家详细讲解有关springSecurity如何自定义用户认证,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为纱窗等企业提供专业服务。

首先我需要在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;  }}

关于“springSecurity如何自定义用户认证”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网站题目:springSecurity如何自定义用户认证
文章来源:http://cdkjz.cn/article/ipehee.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220