Skip to content

Commit

Permalink
用户加入昵称与性别字段,个人中心优化,可修改基本资料
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Nov 30, 2019
1 parent 4c90303 commit 986b146
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ public class SecurityProperties {
/** 必须使用最少88位的Base64对该令牌进行编码 */
private String base64Secret;

/** 令牌过期时间 此处单位/ */
/** 令牌过期时间 此处单位/毫秒 */
private Long tokenValidityInSeconds;

/** 记住我模式下的令牌过期时间 此处单位/毫秒 */
private Long tokenValidityInSecondsForRememberMe;

/** 在线用户 key,根据 key 查询 redis 中在线用户的数据 */
private String onlineKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ public ResponseEntity login(@Validated @RequestBody AuthUser authUser, HttpServl

Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
boolean rememberMe = (authUser.getRememberMe() == null) ? false : authUser.getRememberMe();
// 生成令牌
String token = tokenProvider.createToken(authentication, rememberMe);
String token = tokenProvider.createToken(authentication);
final JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
// 保存在线信息
onlineUserService.save(jwtUser, token, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,13 @@ public void afterPropertiesSet() {
this.key = Keys.hmacShaKeyFor(keyBytes);
}

public String createToken(Authentication authentication, boolean rememberMe) {
public String createToken(Authentication authentication) {
String authorities = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(","));

long now = (new Date()).getTime();
Date validity;
if (rememberMe) {
validity = new Date(now + properties.getTokenValidityInSecondsForRememberMe());
} else {
validity = new Date(now + properties.getTokenValidityInSeconds());
}
Date validity = new Date(now + properties.getTokenValidityInSeconds());

return Jwts.builder()
.setSubject(authentication.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class AuthUser {
@NotBlank
private String password;

private Boolean rememberMe;

private String code;

private String uuid = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
@AllArgsConstructor
public class JwtUser implements UserDetails {

@JsonIgnore
private final Long id;

private final String username;

private final String nickName;

private final String sex;

@JsonIgnore
private final String password;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class OnlineUser {

private String userName;

private String nickName;

private String job;

private String browser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void save(JwtUser jwtUser, String token, HttpServletRequest request){
String address = StringUtils.getCityInfo(ip);
OnlineUser onlineUser = null;
try {
onlineUser = new OnlineUser(jwtUser.getUsername(), job, browser , ip, address, EncryptUtils.desEncrypt(token), new Date());
onlineUser = new OnlineUser(jwtUser.getUsername(), jwtUser.getNickName(), job, browser , ip, address, EncryptUtils.desEncrypt(token), new Date());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private UserDetails createJwtUser(UserDto user) {
return new JwtUser(
user.getId(),
user.getUsername(),
user.getNickName(),
user.getSex(),
user.getPassword(),
user.getAvatar(),
user.getEmail(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public class User implements Serializable {
@Column(unique = true)
private String username;

/** 用户昵称 */
@NotBlank
private String nickName;

/** 性别 */
private String sex;

@OneToOne
@JoinColumn(name = "avatar_id")
private UserAvatar userAvatar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ public ResponseEntity update(@Validated(User.Update.class) @RequestBody User res
return new ResponseEntity(HttpStatus.NO_CONTENT);
}

@Log("修改用户:个人中心")
@ApiOperation("修改用户:个人中心")
@PutMapping(value = "center")
public ResponseEntity center(@Validated(User.Update.class) @RequestBody User resources){
UserDto userDto = userService.findByName(SecurityUtils.getUsername());
if(!resources.getId().equals(userDto.getId())){
throw new BadRequestException("不能修改他人资料");
}
userService.updateCenter(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}

@Log("删除用户")
@ApiOperation("删除用户")
@DeleteMapping(value = "/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ public interface UserService {
* @throws IOException /
*/
void download(List<UserDto> queryAll, HttpServletResponse response) throws IOException;

/**
* 用户自助修改资料
* @param resources /
*/
void updateCenter(User resources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class UserDto implements Serializable {

private String username;

private String nickName;

private String sex;

private String avatar;

private String email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UserQueryCriteria implements Serializable {
@Query(propName = "id", type = Query.Type.IN, joinName = "dept")
private Set<Long> deptIds;

@Query(blurry = "email,username")
@Query(blurry = "email,username,nickName")
private String blurry;

@Query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public void update(User resources) {
user.setDept(resources.getDept());
user.setJob(resources.getJob());
user.setPhone(resources.getPhone());
user.setNickName(resources.getNickName());
user.setSex(resources.getSex());
userRepository.save(user);
}

@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void updateCenter(User resources) {
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
user.setNickName(resources.getNickName());
user.setPhone(resources.getPhone());
user.setSex(resources.getSex());
userRepository.save(user);
}

Expand Down
6 changes: 2 additions & 4 deletions eladmin-system/src/main/resources/config/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ spring:
#jwt
jwt:
header: Authorization
# 令牌前缀,主要最后留个空格
# 令牌前缀
token-start-with: Bearer
# 必须使用最少88位的Base64对该令牌进行编码
base64-secret: ZmQ0ZGI5NjQ0MDQwY2I4MjMxY2Y3ZmI3MjdhN2ZmMjNhODViOTg1ZGE0NTBjMGM4NDA5NzYxMjdjOWMwYWRmZTBlZjlhNGY3ZTg4Y2U3YTE1ODVkZDU5Y2Y3OGYwZWE1NzUzNWQ2YjFjZDc0NGMxZWU2MmQ3MjY1NzJmNTE0MzI=
# 令牌过期时间 此处单位/ ,默认4小时
# 令牌过期时间 此处单位/毫秒 ,默认4小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds: 14400000
# 记住我模式下的令牌过期时间 此处单位/毫秒 ,默认1天
token-validity-in-seconds-for-remember-me: 86400000
# 在线用户key
online-key: online-token
# 验证码
Expand Down
8 changes: 3 additions & 5 deletions eladmin-system/src/main/resources/config/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ spring:
#jwt
jwt:
header: Authorization
# 令牌前缀,主要最后留个空格
# 令牌前缀
token-start-with: Bearer
# 必须使用最少88位的Base64对该令牌进行编码
base64-secret: ZmQ0ZGI5NjQ0MDQwY2I4MjMxY2Y3ZmI3MjdhN2ZmMjNhODViOTg1ZGE0NTBjMGM4NDA5NzYxMjdjOWMwYWRmZTBlZjlhNGY3ZTg4Y2U3YTE1ODVkZDU5Y2Y3OGYwZWE1NzUzNWQ2YjFjZDc0NGMxZWU2MmQ3MjY1NzJmNTE0MzI=
# 令牌过期时间 此处单位/秒 ,默认4小时
token-validity-in-seconds: 14400000
# 记住我模式下的令牌过期时间 此处单位/毫秒 ,默认1天
token-validity-in-seconds-for-remember-me: 86400000
# 令牌过期时间 此处单位/毫秒 ,默认2小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds: 7200000
# 在线用户key
online-key: online-token
# 验证码
Expand Down
3 changes: 0 additions & 3 deletions sql/eladmin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ INSERT INTO `menu` VALUES (3, b'0', '角色管理', 'system/role/index', 1, 3, '
INSERT INTO `menu` VALUES (5, b'0', '菜单管理', 'system/menu/index', 1, 5, 'menu', 'menu', b'0', b'0', 'Menu', '2018-12-18 15:17:28', 'menu:list', 1);
INSERT INTO `menu` VALUES (6, b'0', '系统监控', NULL, 0, 10, 'monitor', 'monitor', b'0', b'0', NULL, '2018-12-18 15:17:48', NULL, 0);
INSERT INTO `menu` VALUES (7, b'0', '操作日志', 'monitor/log/index', 6, 11, 'log', 'logs', b'0', b'0', 'Log', '2018-12-18 15:18:26', NULL, 1);
INSERT INTO `menu` VALUES (8, b'0', '系统缓存', 'monitor/redis/index', 6, 15, 'redis', 'redis', b'0', b'0', 'Redis', '2018-12-18 15:19:01', 'redis:list', 1);
INSERT INTO `menu` VALUES (9, b'0', 'SQL监控', 'monitor/sql/index', 6, 18, 'sqlMonitor', 'druid', b'0', b'0', 'Sql', '2018-12-18 15:19:34', NULL, 1);
INSERT INTO `menu` VALUES (10, b'0', '组件管理', NULL, 0, 50, 'zujian', 'components', b'0', b'0', NULL, '2018-12-19 13:38:16', NULL, 0);
INSERT INTO `menu` VALUES (11, b'0', '图标库', 'components/icons/index', 10, 51, 'icon', 'icon', b'0', b'0', 'Icons', '2018-12-19 13:38:49', NULL, 1);
Expand Down Expand Up @@ -603,7 +602,6 @@ INSERT INTO `roles_menus` VALUES (3, 1);
INSERT INTO `roles_menus` VALUES (5, 1);
INSERT INTO `roles_menus` VALUES (6, 1);
INSERT INTO `roles_menus` VALUES (7, 1);
INSERT INTO `roles_menus` VALUES (8, 1);
INSERT INTO `roles_menus` VALUES (9, 1);
INSERT INTO `roles_menus` VALUES (10, 1);
INSERT INTO `roles_menus` VALUES (11, 1);
Expand Down Expand Up @@ -670,7 +668,6 @@ INSERT INTO `roles_menus` VALUES (2, 2);
INSERT INTO `roles_menus` VALUES (3, 2);
INSERT INTO `roles_menus` VALUES (5, 2);
INSERT INTO `roles_menus` VALUES (6, 2);
INSERT INTO `roles_menus` VALUES (8, 2);
INSERT INTO `roles_menus` VALUES (9, 2);
INSERT INTO `roles_menus` VALUES (10, 2);
INSERT INTO `roles_menus` VALUES (11, 2);
Expand Down

0 comments on commit 986b146

Please sign in to comment.