|
@@ -5,12 +5,23 @@ package cn.seecoder.fdroidrepository.Controller;
|
|
|
import cn.seecoder.fdroidrepository.DataObject.User;
|
|
import cn.seecoder.fdroidrepository.DataObject.User;
|
|
|
import cn.seecoder.fdroidrepository.Service.ServiceImpl.USER_STATUS;
|
|
import cn.seecoder.fdroidrepository.Service.ServiceImpl.USER_STATUS;
|
|
|
import cn.seecoder.fdroidrepository.Service.UserService;
|
|
import cn.seecoder.fdroidrepository.Service.UserService;
|
|
|
-import cn.seecoder.fdroidrepository.utils.Result;
|
|
|
|
|
|
|
+import cn.seecoder.fdroidrepository.config.WebSecurityConfig;
|
|
|
|
|
+import cn.seecoder.fdroidrepository.result.Code;
|
|
|
|
|
+import cn.seecoder.fdroidrepository.result.SingleResult;
|
|
|
|
|
+import cn.seecoder.fdroidrepository.utils.JwtUtil;
|
|
|
|
|
+import cn.seecoder.fdroidrepository.result.Result;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
+import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/api/users")
|
|
@RequestMapping("/api/users")
|
|
@@ -18,12 +29,17 @@ public class UserController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private UserService userService;
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private JwtUtil jwtTokenUtils;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
|
|
+
|
|
|
@PostMapping("/register")
|
|
@PostMapping("/register")
|
|
|
public ResponseEntity<String> register(@RequestBody User user) {
|
|
public ResponseEntity<String> register(@RequestBody User user) {
|
|
|
if (!userService.isUsernameUnique(user.getUsername())) {
|
|
if (!userService.isUsernameUnique(user.getUsername())) {
|
|
|
return new ResponseEntity<>("Username is already taken", HttpStatus.BAD_REQUEST);
|
|
return new ResponseEntity<>("Username is already taken", HttpStatus.BAD_REQUEST);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
userService.save(user);
|
|
userService.save(user);
|
|
|
return new ResponseEntity<>("User registered successfully", HttpStatus.OK);
|
|
return new ResponseEntity<>("User registered successfully", HttpStatus.OK);
|
|
|
}
|
|
}
|
|
@@ -40,13 +56,25 @@ public class UserController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
@PostMapping("/login")
|
|
|
- public ResponseEntity<String> login(@RequestBody User user) {
|
|
|
|
|
- User existingUser = userService.findByUsername(user.getUsername());
|
|
|
|
|
|
|
+ public Result<String> login(HttpServletResponse response, @RequestBody User user) {
|
|
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
|
|
|
|
|
+ user.getUsername(), user.getPassword());
|
|
|
|
|
|
|
|
- if (existingUser == null || !existingUser.getPassword().equals(user.getPassword())) {
|
|
|
|
|
- return new ResponseEntity<>("Invalid username or password", HttpStatus.UNAUTHORIZED);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ //AuthenticationManager(default ProviderManager) #authenticate check Authentication
|
|
|
|
|
+ Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
|
|
+ //bind authentication to securityContext
|
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
|
+ //create token
|
|
|
|
|
+ String token = jwtTokenUtils.createToken(authentication);
|
|
|
|
|
|
|
|
- return new ResponseEntity<>("Login successful", HttpStatus.OK);
|
|
|
|
|
|
|
+ String authHeader = WebSecurityConfig.TOKEN_PREFIX + token;
|
|
|
|
|
+ //put token into http header
|
|
|
|
|
+ response.addHeader(WebSecurityConfig.AUTHORIZATION_HEADER, authHeader);
|
|
|
|
|
+
|
|
|
|
|
+ return SingleResult.success(authHeader);
|
|
|
|
|
+ } catch (BadCredentialsException authentication) {
|
|
|
|
|
+ return SingleResult.failure(Code.LOGIN_FAILED);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|