This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/sport/sportsmailserver/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.sport.sportsmailserver.controller; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.sport.sportsmailserver.dto.RestModel; | ||
import com.sport.sportsmailserver.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author itning | ||
* @date 2020/2/12 11:39 | ||
*/ | ||
@RestController | ||
public class UserController { | ||
private final UserService userService; | ||
|
||
@Autowired | ||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
/** | ||
* 用户登录 | ||
* | ||
* @param username 用户名 | ||
* @param password 密码 | ||
* @return ResponseEntity | ||
* @throws JsonProcessingException See {@link UserService#login(String, String)} | ||
*/ | ||
@PostMapping("/login") | ||
public ResponseEntity<?> login(@RequestParam String username, | ||
@RequestParam String password) throws JsonProcessingException { | ||
return RestModel.ok(userService.login(username, password)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/sport/sportsmailserver/exception/ErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.sport.sportsmailserver.exception; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.web.servlet.error.ErrorAttributes; | ||
import org.springframework.boot.web.servlet.error.ErrorController; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Map; | ||
|
||
/** | ||
* 错误处理器 | ||
* | ||
* @author itning | ||
*/ | ||
@RestController | ||
public class ErrorHandler implements ErrorController { | ||
private final ErrorAttributes errorAttributes; | ||
|
||
@Autowired | ||
public ErrorHandler(ErrorAttributes errorAttributes) { | ||
this.errorAttributes = errorAttributes; | ||
} | ||
|
||
@Override | ||
public String getErrorPath() { | ||
return "/error"; | ||
} | ||
|
||
@RequestMapping(value = "/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | ||
public String error(HttpServletRequest request) { | ||
WebRequest webRequest = new ServletWebRequest(request); | ||
Map<String, Object> errorAttributes = this.errorAttributes.getErrorAttributes(webRequest, true); | ||
String msg = errorAttributes.getOrDefault("error", "not found").toString(); | ||
String code = errorAttributes.getOrDefault("status", 404).toString(); | ||
return "{\"code\":" + code + ",\"msg\":\"" + msg + "\",\"data\":\"\"}"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sport/sportsmailserver/exception/IdNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sport.sportsmailserver.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
/** | ||
* 主键不存在 | ||
* | ||
* @author itning | ||
* @date 2020/2/12 11:34 | ||
*/ | ||
public class IdNotFoundException extends BaseException { | ||
public IdNotFoundException(String msg) { | ||
super(msg, HttpStatus.NOT_FOUND); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sport/sportsmailserver/exception/NullFiledException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sport.sportsmailserver.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
/** | ||
* 传入的参数为空 | ||
* | ||
* @author itning | ||
* @date 2020/2/12 11:34 | ||
*/ | ||
public class NullFiledException extends BaseException { | ||
public NullFiledException(String msg) { | ||
super(msg, HttpStatus.BAD_REQUEST); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sport/sportsmailserver/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sport.sportsmailserver.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
/** | ||
* 用户服务 | ||
* | ||
* @author itning | ||
* @date 2020/2/12 11:29 | ||
*/ | ||
public interface UserService { | ||
/** | ||
* 用户登录 | ||
* | ||
* @param username 用户名 | ||
* @param password 密码 | ||
* @return jwt token | ||
* @throws JsonProcessingException JWT构建失败 | ||
*/ | ||
String login(String username, String password) throws JsonProcessingException; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/sport/sportsmailserver/service/impl/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.sport.sportsmailserver.service.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.sport.sportsmailserver.dto.LoginUser; | ||
import com.sport.sportsmailserver.entity.Role; | ||
import com.sport.sportsmailserver.entity.User; | ||
import com.sport.sportsmailserver.exception.IdNotFoundException; | ||
import com.sport.sportsmailserver.exception.NullFiledException; | ||
import com.sport.sportsmailserver.exception.SecurityServerException; | ||
import com.sport.sportsmailserver.repository.RoleRepository; | ||
import com.sport.sportsmailserver.repository.UserRepository; | ||
import com.sport.sportsmailserver.service.UserService; | ||
import com.sport.sportsmailserver.util.JwtUtils; | ||
import com.sport.sportsmailserver.util.OrikaUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* @author itning | ||
* @date 2020/2/12 11:31 | ||
*/ | ||
@Service | ||
@Transactional(rollbackFor = Exception.class) | ||
public class UserServiceImpl implements UserService { | ||
private final UserRepository userRepository; | ||
|
||
@Autowired | ||
public UserServiceImpl(UserRepository userRepository, RoleRepository roleRepository) { | ||
this.userRepository = userRepository; | ||
if (!roleRepository.existsById(Role.ROLE_ADMIN_ID)) { | ||
Role role = new Role(); | ||
role.setId(Role.ROLE_ADMIN_ID); | ||
role.setName("管理员"); | ||
roleRepository.save(role); | ||
} | ||
if (!roleRepository.existsById(Role.ROLE_USER_ID)) { | ||
Role role = new Role(); | ||
role.setId(Role.ROLE_USER_ID); | ||
role.setName("用户"); | ||
roleRepository.save(role); | ||
} | ||
} | ||
|
||
@Override | ||
public String login(String username, String password) throws JsonProcessingException { | ||
if (StringUtils.isAnyBlank(username, password)) { | ||
throw new NullFiledException("用户名/密码不能为空"); | ||
} | ||
User user = userRepository.findById(username).orElseThrow(() -> new IdNotFoundException("用户名不存在")); | ||
if (!password.equals(user.getPassword())) { | ||
throw new SecurityServerException("密码错误", HttpStatus.BAD_REQUEST); | ||
} | ||
LoginUser loginUser = OrikaUtils.a2b(user, LoginUser.class); | ||
return JwtUtils.buildJwt(loginUser); | ||
} | ||
} |