Java achieve with spring login authentication security framework

package com.offcn.core.service;

import com.offcn.core.brand.SellerService;

import com.offcn.core.pojo.seller.Seller;
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;

public class UserDetailServiceImpl implements UserDetailsService {

private SellerService sellerService;

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

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    ArrayList<GrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
    //判断用户名是否为空
    if (username==null){
        return null;
    }
    //查询表里是否有这个数据
    Seller seller = sellerService.findOne(username);
    //判断商家的审核状态是否通过
    if ("1".equals(seller.getStatus())){
              //返回的user对象包含账号,密码,
              access:设置角色  角色命名 ROLE_角色名称  如:  ROLE_USER  
              //	<intercept-url pattern="/**" access="ROLE_SELLER"/>
              就是只有这个角色才不拦截,
              返回对象
        return  new User(username,seller.getPassword(),authorities);
    }

    return null;
}

}

Published 33 original articles · won praise 0 · Views 876

Guess you like

Origin blog.csdn.net/ninth_spring/article/details/103616575