Java atteindre avec cadre de sécurité d'authentification de connexion de ressort

emballer com.offcn.core.service;

importation com.offcn.core.brand.SellerService;

importation com.offcn.core.pojo.seller.Seller;
importation org.springframework.security.core.GrantedAuthority;
importation org.springframework.security.core.authority.SimpleGrantedAuthority;
importation org.springframework.security.core.userdetails.User;
org.springframework.security.core.userdetails.UserDetails à l'importation;
importation org.springframework.security.core.userdetails.UserDetailsService;
importation org.springframework.security.core.userdetails.UsernameNotFoundException;

importation java.util.ArrayList;

public class {UserDetailServiceImpl outils 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;
}

}

Publié 33 articles originaux · louanges gagnées 0 · Vues 876

Je suppose que tu aimes

Origine blog.csdn.net/ninth_spring/article/details/103616575
conseillé
Classement