Compare commits
2 Commits
275ca89962
...
89335ea55b
Author | SHA1 | Date | |
---|---|---|---|
89335ea55b | |||
98d938976b |
@ -1,6 +1,7 @@
|
||||
package github.benjamin.equipreservebackend.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@ -12,4 +13,13 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
registry.addResourceHandler("/device_image/**")
|
||||
.addResourceLocations("file:" + System.getProperty("user.dir") + "/device_image/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedMethods("*")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package github.benjamin.equipreservebackend.controller;
|
||||
|
||||
import github.benjamin.equipreservebackend.dto.LoginRequest;
|
||||
import github.benjamin.equipreservebackend.entity.Role;
|
||||
import github.benjamin.equipreservebackend.vo.LoginResponse;
|
||||
import github.benjamin.equipreservebackend.response.ResponseResult;
|
||||
import github.benjamin.equipreservebackend.entity.User;
|
||||
@ -8,8 +10,11 @@ import github.benjamin.equipreservebackend.service.UserService;
|
||||
import github.benjamin.equipreservebackend.utils.JwtUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
@ -19,10 +24,11 @@ public class UserController {
|
||||
private final JwtUtil jwtUtil;
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseResult<LoginResponse> login(String username, String password) {
|
||||
User user = userService.login(username, password);
|
||||
public ResponseResult<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
User user = userService.login(request.getUsername(), request.getPassword());
|
||||
SecurityUser securityUser = userService.loadSecurityUserById(user.getId());
|
||||
String token = jwtUtil.generateToken(securityUser);
|
||||
return ResponseResult.success(new LoginResponse(user.getId(), user.getName(), token));
|
||||
List<String> roles = securityUser.getRoles().stream().map(Role::getCode).toList();
|
||||
return ResponseResult.success(new LoginResponse(user.getId(), user.getName(), roles, token));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package github.benjamin.equipreservebackend.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginRequest {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
}
|
@ -1,14 +1,18 @@
|
||||
package github.benjamin.equipreservebackend.vo;
|
||||
|
||||
import github.benjamin.equipreservebackend.entity.Role;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class LoginResponse {
|
||||
|
||||
private Long userId;
|
||||
private String name;
|
||||
private List<String> roles;
|
||||
private String token;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user