A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Socuss 初级黑马   /  2019-9-12 12:24  /  1200 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Spring security中的系统的User只有username,password,enabled等几个属性,对于实际开发中,这些属性用的概率非常低
比如: 用户查看自己的头像,手机号,修改昵称等等, 在spring-security默认的User对象中并不存在这些自定义的字段.怎么办呢?
这就需要我们自己扩展User类了.

思路:
我自己定义的MyUser 继承了spring-security中的User类,对其进行扩展.

  1. package com.qingcheng.controller;

  2. import org.springframework.security.core.GrantedAuthority;
  3. import org.springframework.security.core.userdetails.User;

  4. import java.util.Collection;

  5. public class MyUser extends User {
  6.     private Integer id;
  7.     private String loginName;
  8.     private String image;

  9.     public MyUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
  10.         super(username, password, authaorities);
  11.     }

  12.     public MyUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
  13.         super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
  14.     }

  15.     public Integer getId() {
  16.         return id;
  17.     }

  18.     public void setId(Integer id) {
  19.         this.id = id;
  20.     }

  21.     public String getLoginName() {
  22.         return loginName;
  23.     }

  24.     public void setLoginName(String loginName) {
  25.         this.loginName = loginName;
  26.     }

  27.     public String getImage() {
  28.         return image;
  29.     }

  30.     public void setImage(String image) {
  31.         this.image = image;
  32.     }
  33. }
复制代码


然后在UserDetailService的实现类UserDetailServiceImpl中,将匹配到的用户信息, set到自定义的MyUser对象中,再返回,如下:
  1. package com.qingcheng.controller;

  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.qingcheng.pojo.system.Admin;
  4. import com.qingcheng.service.system.AdminService;
  5. import org.springframework.security.core.GrantedAuthority;
  6. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  7. import org.springframework.security.core.userdetails.UserDetails;
  8. import org.springframework.security.core.userdetails.UserDetailsService;
  9. import org.springframework.security.core.userdetails.UsernameNotFoundException;

  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;

  14. public class UserDetailsServiceImpl implements UserDetailsService {

  15.     @Reference
  16.     private AdminService adminService;

  17.     @Override
  18.     public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

  19.         Map  searchMap = new HashMap();
  20.         searchMap.put("loginName",s);
  21.         searchMap.put("status","1");
  22.         List<Admin> list = adminService.findList(searchMap);
  23.         if(list.size() == 0){
  24.             return null;
  25.         }

  26.         ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
  27.         grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  28.         Admin admin = list.get(0);
  29. //        return new User(s,list.get(0).getPassword(),grantedAuthorities);
  30.         MyUser myUser = new MyUser(s, admin.getPassword(), grantedAuthorities);
  31.         myUser.setId(admin.getId());
  32.         myUser.setLoginName(admin.getLoginName());
  33.         myUser.setImage(admin.getImage());
  34.         return myUser;
  35.     }
  36. }
复制代码


第一次发帖, 也是java小白, 不要注重于代码,写得比较low.谢谢哈!

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马