This commit is contained in:
kim 2024-12-27 15:31:56 +09:00
parent e403c096b2
commit ffb58d1709
25 changed files with 217 additions and 107 deletions

40
pom.xml
View File

@ -77,20 +77,30 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL 드라이버 -->
<!-- <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- JWT -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<!-- ModelMapper -->
<dependency>
@ -111,6 +121,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

View File

@ -0,0 +1,11 @@
package com.example.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
}

View File

@ -0,0 +1,24 @@
package com.example.backend.config;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class JwtTokenProvider {
private final String SECRET_KEY = "secret-key";
public String createToken(String username, Long userId) {
return Jwts.builder()
.setSubject(username)
.claim("userId", userId)
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + 86400000))
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
.compact();
}
public Long getUserIdFromToken(String token) {
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody().get("userId", Long.class);
}
}

View File

@ -0,0 +1,5 @@
package com.example.backend.config;
public class SecurityConfig {
}

View File

@ -0,0 +1,19 @@
package com.example.backend.controller;
import com.example.backend.dto.LoginRequest;
import com.example.backend.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
private AuthService authService;
@PostMapping("/login")
public String login(@RequestBody LoginRequest request) {
return authService.login(request.getUsername(), request.getPassword());
}
}

View File

@ -0,0 +1,5 @@
package com.example.backend.controller;
public class ImageController {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.controller;
public class PermissionController {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.controller;
public class StateController {
}

View File

@ -0,0 +1,11 @@
package com.example.backend.dto;
import lombok.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginRequest {
private String username;
private String password;
}

View File

@ -0,0 +1,5 @@
package com.example.backend.entity;
public class Image {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.entity;
public class Permissions {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.entity;
public class State {
}

View File

@ -0,0 +1,25 @@
package com.example.backend.entity;
import java.util.Date;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
private Date lastLogin;
}

View File

@ -0,0 +1,5 @@
package com.example.backend.repository;
public class ImageRepository {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.repository;
public class PermissionRepository {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.repository;
public class StateRepository {
}

View File

@ -0,0 +1,9 @@
package com.example.backend.repository;
import com.example.backend.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
}

View File

@ -0,0 +1,25 @@
package com.example.backend.service;
import com.example.backend.config.JwtTokenProvider;
import com.example.backend.entity.User;
import com.example.backend.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class AuthService {
@Autowired
private UserRepository userRepository;
@Autowired
private JwtTokenProvider jwtTokenProvider;
public String login(String username, String password) {
User user = userRepository.findByUsername(username).orElseThrow(() -> new RuntimeException("User not found"));
if (!new BCryptPasswordEncoder().matches(password, user.getPassword())) {
throw new RuntimeException("Invalid credentials");
}
return jwtTokenProvider.createToken(user.getUsername(), user.getId());
}
}

View File

@ -0,0 +1,5 @@
package com.example.backend.service;
public class ImageService {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.service;
public class PermissionService {
}

View File

@ -0,0 +1,5 @@
package com.example.backend.service;
public class StateService {
}

View File

@ -1,24 +0,0 @@
package com.example.demo.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.AuthService;
@RestController
@RequestMapping("/api/auth")
public class authController {
private final AuthService authService;
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest) {
String token = authService.authenticate(loginRequest.getUsername(), loginRequest.getPassword());
return ResponseEntity.ok(token);
}
}
/* 안녕하세요 IPP 교육생 김대훈입니다.
1월 8일() IPP 관련 학교 행사에 참여하게 되어서 해당 날짜에 출근하지 않고 바로 행사장소로 간다고 합니다.
해당 사안에 관해서 학교와 인사 담당자님과 이야기 됐다고 합니다.
인사 담당자님께서 담당교사님께 전달하라고 하셔서 이렇게 메시지 보냅니다! 감사합니다. */

View File

@ -1,34 +0,0 @@
package com.example.demo.entity;
import java.time.LocalDateTime;
import javax.swing.SwingWorker.StateValue;
import jakarta.persistence.*;
@Entity
@Table(name = "states")
public class stateEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long stateId;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private userEntity user;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private StateValue stateValue;
private Double gpsLatitude;
private Double gpsLongitude;
private String address;
@Column(nullable = false)
private LocalDateTime updatedAt = LocalDateTime.now();
// Getters and Setters
}

View File

@ -1,32 +0,0 @@
package com.example.demo.entity;
import java.time.LocalDateTime;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")
public class userEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
private LocalDateTime lastLogin;
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt = LocalDateTime.now();
}

View File

@ -1,5 +0,0 @@
package com.example.demo.service;
public class AuthService {
}