Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize user authentication by springSecurity

Shulou Source: shulou.com Published: 2022-06-03 09:35:57 10月03日 Update

This article will explain in detail how to customize user authentication in springSecurity. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

First of all, I need to declare in the xml file. I want to customize the user's authentication class, that is, I want to query from the database myself.

After configuring the custom file, implement it in the module that needs the custom authentication class.

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;/** * custom authentication class * @ Auther:qingmu * @ Description: down-to-earth, only to stand out * @ 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 vendor approval) {/ / determine whether a merchant is approved or not. If ("1" .equals (seller.getStatus () {/ / create a collection to store permissions HashSet authorities = new HashSet (); authorities.add (new SimpleGrantedAuthority ("ROLE_SELLER")); / / return the user's information to the authentication class return new User (username,seller.getPassword (), authorities);}} / / if there is no such user, return null return null;} public UserDetailServiceImpl (SellerService sellerService) {this.sellerService = sellerService } public SellerService getSellerService () {return sellerService;} public void setSellerService (SellerService sellerService) {this.sellerService = sellerService;}}

This is the end of this article on "how to customize springSecurity user authentication". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Authentication users articles I want files more good practical stand out down-to-earth that is information content merchants field data databases articles time permissions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Docker Shulou Tech Info vpn