Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 토큰 키 이름 변경 #40

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/com/hanaro/starbucks/config/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class JwtUtil {

@PostConstruct
protected void init() {
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes());
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes()); // secret key 암호화
}

public String createToken(String userId, List<String> roleList) {
Expand All @@ -51,11 +51,12 @@ public Authentication getAuthentication(String token) {
UserDetails userDetails = userDetailsService.loadUserByUsername(userId);
return new UsernamePasswordAuthenticationToken(
userDetails, "", userDetails.getAuthorities()
);
); // 사용자 정보를 기반으로 토큰 생성
}

// http 요청에서 "TOKEN" 헤더 값을 읽어오는 메소드
public String resolveToken(HttpServletRequest request) {
return request.getHeader("X-AUTH-TOKEN");
return request.getHeader("TOKEN");
}

public boolean validateToken(String token){
Expand Down
43 changes: 18 additions & 25 deletions src/main/java/com/hanaro/starbucks/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hanaro.starbucks.config;

import com.hanaro.starbucks.service.impl.UserDetailsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -27,37 +28,29 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.csrf((auth) -> auth.disable())
.cors((cors) -> cors.configurationSource(corsConfigurationSource()))

// .authorizeHttpRequests((auth) -> auth
// // 로그인과 회원가입은 모든 사용자에게 허용한다.
// .requestMatchers(
// new AntPathRequestMatcher("/login"),
// new AntPathRequestMatcher("/signup")
// ).permitAll() // 권한이 있든 말든 모두 접근 가능
// // admin일 경우에만 /admin에 대한 요청에서 접근을 허용한다.
// .requestMatchers("/admin").hasRole("ADMIN")
// // 그 외 모든 요청은 인증된 사용자에게만 허용한다.
// .anyRequest().authenticated()
// )
// .formLogin((formLogin) -> formLogin
// .loginProcessingUrl("/login")
// .successHandler(((request, response, authentication) -> {
// System.out.println("로그인 성공했습니다.");
// response.sendRedirect("/");
// }))
// .permitAll()
// );
.authorizeHttpRequests((auth) -> auth
// 로그인과 회원가입은 모든 사용자에게 허용한다.
.requestMatchers(
new AntPathRequestMatcher("/api/v1/users/login"),
new AntPathRequestMatcher("/api/v1/users/signup")
).permitAll() // 권한이 있든 말든 모두 접근 가능
// admin일 경우에만 /admin에 대한 요청에서 접근을 허용한다.
.requestMatchers("/api/v1/users/admin/**").hasRole("ADMIN")
.requestMatchers("/api/v1/products/admin/**").hasRole("ADMIN")
.requestMatchers("/api/v1/orders/admin/**").hasRole("ADMIN")
// 그 외 모든 요청은 인증된 사용자에게만 허용한다.
.anyRequest().permitAll()
)

.authorizeHttpRequests( (auth) -> auth
.requestMatchers( new AntPathRequestMatcher("/**") )
.permitAll())
// .authorizeHttpRequests( (auth) -> auth
// .requestMatchers( new AntPathRequestMatcher("/**") )
// .permitAll())

.sessionManagement(sessionManagement ->
sessionManagement.sessionCreationPolicy(
SessionCreationPolicy.STATELESS // 세션에 저장하지 않겠다. 기본이 세션에 저장하는 것
))
.addFilterBefore(new JwtAuthenticationFilter(jwtConfig),
UsernamePasswordAuthenticationFilter.class);

.addFilterBefore(new JwtAuthenticationFilter(jwtConfig), UsernamePasswordAuthenticationFilter.class);

return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hanaro.starbucks.util.APIConstant.API_VERSION;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/users")
@RequestMapping(API_VERSION + "/users")
public class MemberController {
private final MemberService memberService;
private final JwtUtil jwtUtil;

@GetMapping("")
@GetMapping("/admin")
public List<MemberResDto> getUsers(){
return memberService.getUsers();
}
@GetMapping("/{userIdx}")
@GetMapping("/admin/{userIdx}")
public MemberResDto getUser(@PathVariable int userIdx){
return memberService.getUser(userIdx);
}

@PostMapping("/signup")
@ResponseBody
public ResponseEntity<?> signup(@RequestBody SignupReqDto user) {
System.out.println(user);
boolean findUser = memberService.findUserByUserId(user.getUserId());
System.out.println(findUser);
if (!findUser) {
MemberResDto newUser = memberService.createUser(user);
return ResponseEntity.ok(newUser.getUserId());
Expand All @@ -44,9 +46,13 @@ public ResponseEntity<?> signup(@RequestBody SignupReqDto user) {
@ResponseBody
public ResponseEntity<?> login(@RequestBody LoginReqDto user) {
MemberResDto findUser = memberService.findUserByUserIdAndUserPw(user.getUserId(), user.getUserPw());
System.out.println(findUser);
if (findUser != null) {
String token = jwtUtil.createToken(findUser.getUserId(), Arrays.asList(findUser.getUserRole()));
return ResponseEntity.ok(token);
Map<String, Object> response = new HashMap<>();
response.put("user", findUser); // 사용자 정보
response.put("token", token); // 토큰
return ResponseEntity.ok(response);
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("로그인에 실패하였습니다. 아이디와 비밀번호를 확인해주세요.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hanaro.starbucks.dto.orders.OrderEditReqDto;
import com.hanaro.starbucks.dto.orders.OrderResDto;
import com.hanaro.starbucks.service.OrderService;
import io.jsonwebtoken.Jwts;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -21,6 +22,7 @@ public class OrderController {
public List<OrderResDto> getOrders() {
return orderService.getOrders();
}

@GetMapping("/{orderIdx}")
public OrderResDto getOrder(@PathVariable int orderIdx) {
return orderService.getOrder(orderIdx);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hanaro.starbucks.dto.member;

import com.hanaro.starbucks.entity.Member;
import com.hanaro.starbucks.enumeration.UserRole;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -30,7 +31,7 @@ public MemberResDto(Member user){
this.userId = user.getUserId();
this.userPw = user.getUserPw();
this.userNickname = user.getUserNickname();
this.userRole = user.getUserRole();
this.userRole = String.valueOf(user.getUserRole());
this.userPoint = user.getUserPoint();
this.userJoinDate = user.getUserJoinDate();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hanaro.starbucks.dto.member;

import com.hanaro.starbucks.enumeration.UserRole;
import lombok.Getter;

import java.time.LocalDate;
Expand All @@ -9,7 +10,7 @@ public class MemberUpdateReqDto {
private String userId;
private String userPw;
private String userNickname;
private String userRole;
private UserRole userRole;
private Integer userPoint;
private LocalDate userJoinDate;
}
50 changes: 47 additions & 3 deletions src/main/java/com/hanaro/starbucks/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.hanaro.starbucks.entity;

import com.hanaro.starbucks.dto.member.MemberUpdateReqDto;
import com.hanaro.starbucks.enumeration.UserRole;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@NoArgsConstructor
@Table(name = "user")
public class Member {
public class Member implements UserDetails {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,7 +37,8 @@ public class Member {
private String userNickname;

@Column(name = "user_role")
private String userRole;
@Enumerated(EnumType.STRING)
private UserRole userRole;

@Column(name = "user_point")
private int userPoint;
Expand All @@ -42,7 +51,7 @@ public Member(String userId, String userPw, String userNickname) {
this.userId = userId;
this.userPw = userPw;
this.userNickname = userNickname;
this.userRole = "USER";
this.userRole = UserRole.USER;
this.userPoint = 0;
}

Expand All @@ -54,4 +63,39 @@ public void update(MemberUpdateReqDto dto) {
this.userPoint = dto.getUserPoint();
this.userJoinDate = LocalDate.now();
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
//계정의 권한 목록을 리턴함.
Set<GrantedAuthority> roles = new HashSet<>();
roles.add(new SimpleGrantedAuthority(userRole.getValue()));
return roles;
}

@Override
public String getUsername() {
return this.userId; //계정의 고유한 아이디 리턴
}

@Override
public String getPassword() {
return this.userPw; //계정의 암호 리턴
}

@Override
public boolean isAccountNonExpired() {
return true; //계정의 만료 여부 리턴 - true는 사용가능하다는 의미
}
@Override
public boolean isAccountNonLocked() {
return true; //계정의 잠김 여부 리턴
}
@Override
public boolean isCredentialsNonExpired() {
return true; //비밀번호 만료 여부 리턴
}
@Override
public boolean isEnabled() {
return true; //계정의 활성화 여부 리턴
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/hanaro/starbucks/enumeration/UserRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hanaro.starbucks.enumeration;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum UserRole {
USER("ROLE_USER"),
ADMIN("ROLE_ADMIN");

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hanaro.starbucks.service.impl;

import com.hanaro.starbucks.entity.Member;
import com.hanaro.starbucks.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

@RequiredArgsConstructor
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private final MemberRepository memberRepository;

@Override
public Member loadUserByUsername(String userId) throws UsernameNotFoundException {
Optional<Member> optional = Optional.ofNullable(memberRepository.findByUserId(userId).orElseThrow(() -> new BadCredentialsException("아이디에 맞는 회원 정보를 찾을 수 없습니다.")));
return optional.get();
}
}
Loading