-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginController.java
More file actions
67 lines (49 loc) · 2.19 KB
/
Copy pathLoginController.java
File metadata and controls
67 lines (49 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.github.spring.user.controller;
import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.github.spring.common.UserInfo;
import com.github.spring.common.WebServiceException;
import com.github.spring.common.token.TokenService;
@RestController
@RequestMapping(value = "/api/login")
public class LoginController {
@Autowired
private TokenService tokenService;
@Value("${jwt.token.key}")
private String jwtSecret;
@Value("${jwt.user.name}")
private String user;
@Value("${jwt.user.password}")
private String userPassword;
@Value("${jwt.admin.name}")
private String admin;
@Value("${jwt.admin.password}")
private String adminPassword;
List<String> userAuthorities = Arrays.asList("ROLE_USER");
List<String> adminAuthorities = Arrays.asList("ROLE_ADMIN", "ROLE_USER");
@RequestMapping(method = RequestMethod.POST)
public JwtResponse login(@Valid @RequestBody LoginInfo loginInfo) {
return new JwtResponse().setJwtToken(tokenService.encodeToken(validateAndget(loginInfo), jwtSecret));
}
private UserInfo validateAndget(LoginInfo loginInfo) {
if (loginInfo.getUsername().equals(user) && loginInfo.getPassword().equals(userPassword)) {
// if user login
return new UserInfo().setName(user).setAuthorities(userAuthorities);
}
else if (loginInfo.getUsername().equals(admin) && loginInfo.getPassword().equals(adminPassword)) {
// if admin login
return new UserInfo().setName(admin).setAuthorities(adminAuthorities);
}
else {
throw new WebServiceException(HttpStatus.UNAUTHORIZED, "username or password not valid");
}
}
}