This commit is contained in:
2025-05-12 14:19:49 +09:00
50 changed files with 755 additions and 307 deletions

View File

@ -1,37 +0,0 @@
package co.jp.app.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import co.jp.app.entity.ErrorEntity;
import co.jp.app.entity.UserEntity;
import co.jp.app.service.ErraService;
import co.jp.app.service.UserService;
@CrossOrigin("http://192.168.1.50:5173")
@RestController("/api/login")
public class LoginController {
@Autowired
private UserService userService;
@Autowired
private ErraService erraService;
@GetMapping("/status")
public String getStatusByNameOrEmail() {
String input="aaa";
if (userByName == null && userByEmail == null) {
return "全項目に入力してください";
}
// 如果有找到,就固定使用 ID 1001 去查 erraEntity
ErrorEntity erra = erraService.getStatusById(1001);
return erra.getStatus();
}
}

View File

@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import co.jp.app.service.PetService;
@Controller
public class PetController {
@ -19,7 +18,7 @@ public class PetController {
@GetMapping("/api/dogs/pet")
public String getListByEntities(@RequestParam List<Integer> id) {
service.getPetByID(id);
service.getPetByID(id);
return "pet";
}
}

View File

@ -9,20 +9,19 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import co.jp.app.entity.PetEntity;
import co.jp.app.service.UploadService;
@CrossOrigin("http://192.168.1.50:5173")
@Controller
public class UploadController {
@Autowired
private UploadService service;
//private UploadService service;
@PostMapping("/api/dogs/upload")
public String upload() {
List<PetEntity> list = new ArrayList<PetEntity>();
service.saveAll(list);
//service.saveAll(list);
return "upload";

View File

@ -0,0 +1,77 @@
package co.jp.app.controller;
import co.jp.app.common.ApiResponse;
import co.jp.app.dto.LoginDto;
import co.jp.app.dto.RegistrationDto;
import co.jp.app.service.JwtService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
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.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import co.jp.app.entity.ErrorEntity;
import co.jp.app.entity.UserEntity;
import co.jp.app.service.ErraService;
import co.jp.app.service.UserService;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
private final AuthenticationManager authenticationManager;
private final JwtService jwtService;
public UserController(UserService userService, AuthenticationManager authenticationManager, JwtService jwtService) {
this.userService = userService;
this.authenticationManager = authenticationManager;
this.jwtService = jwtService;
}
@PostMapping("/register")
public ResponseEntity<?> registerUser(@Valid @RequestBody RegistrationDto registrationDto) {
try {
UserEntity registeredUser = userService.registerNewUser(registrationDto);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(registeredUser.getEmail()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.fail("ユーザー登録失敗しました。"));
}
}
@PostMapping("/login")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginDto loginDto) {
try {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword())
);
SecurityContextHolder.getContext().setAuthentication(authentication);
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
String jwtToken = jwtService.generateToken(userDetails); // 生成单一的Token
Map<String, String> tokenResponse = new HashMap<>();
tokenResponse.put("token", jwtToken);
return ResponseEntity.ok(ApiResponse.success(tokenResponse));
} catch (BadCredentialsException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ApiResponse.fail("メールアドレスまたはパスワードが間違っています。"));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.fail("サーバーエラー。"));
}
}
}