パッケージcom.offcn.core.service。
輸入com.offcn.core.brand.SellerService。
輸入com.offcn.core.pojo.seller.Seller;
輸入org.springframework.security.core.GrantedAuthority。
輸入org.springframework.security.core.authority.SimpleGrantedAuthority。
輸入org.springframework.security.core.userdetails.User;
輸入org.springframework.security.core.userdetails.UserDetails。
輸入org.springframework.security.core.userdetails.UserDetailsService。
輸入org.springframework.security.core.userdetails.UsernameNotFoundException。
輸入はjava.util.ArrayList;
パブリッククラスUserDetailServiceImpl実装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;
}
}