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