Skip to content

Commit

Permalink
[#42] Feat: Admin 로그인 기능 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
SY2on committed Jan 20, 2024
1 parent 7b13fb5 commit 0a6341a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package ssu.eatssu.domain.admin.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import ssu.eatssu.domain.admin.dto.LoginRequest;
import ssu.eatssu.domain.admin.service.AuthenticationService;
import ssu.eatssu.domain.user.dto.Tokens;
import ssu.eatssu.global.handler.response.BaseResponse;

@Slf4j
@Controller
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AuthenticationController {

private final AuthenticationService authenticationService;

@GetMapping("/login")
public String loginPage(Model model) {
public String loginPage() {
return "login";
}

@ResponseBody
@PostMapping("/login")
public BaseResponse<Tokens> login(@RequestBody LoginRequest request) {
return BaseResponse.success(authenticationService.login(request.password()));
}

}
4 changes: 4 additions & 0 deletions src/main/java/ssu/eatssu/domain/admin/dto/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ssu.eatssu.domain.admin.dto;

public record LoginRequest(String password) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ssu.eatssu.domain.admin.service;

import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import ssu.eatssu.domain.admin.controller.AdminAuth;
import ssu.eatssu.domain.auth.security.JwtTokenProvider;
import ssu.eatssu.domain.user.dto.Tokens;
import ssu.eatssu.domain.user.entity.User;
import ssu.eatssu.domain.user.repository.UserRepository;

@RequiredArgsConstructor
@Service
public class AuthenticationService {
private final JwtTokenProvider tokenProvider;
private final PasswordEncoder passwordEncoder;
private final UserRepository userRepository;
private final AdminAuth adminAuth;

public Tokens login(String password) {
return tokenProvider.generateTokens(adminAuth.loginId(), password);
}

private void join(String loginId, String password) {
String credentials = createCredentials(password);

//회원가입
User user = User.adminJoin(loginId, credentials);
userRepository.save(user);
}

private String createCredentials(String password) {
return passwordEncoder.encode(password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private static final String[] RESOURCE_LIST = {
"/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**","/admin/img/**","/css/**", "/js/**",
"/favicon.ico", "/error/**", "/webjars/**", "/h2-console/**"
};

private static final String[] AUTH_WHITELIST = {
"/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**",
"/", "/user/join", "/user/login", "/user/user-emails/{email}/exist", "/user/check-nickname",
"/menu/**", "/restaurants/**", "/review/info","/review/list", "/oauth/**", "/inquiries/{userInquiriesId}",
"/inquiries/list"
"/inquiries/list", "/admin/login"
};

private static final String[] ADMIN_PAGE_LIST = {
Expand All @@ -48,6 +51,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(authorize -> authorize
.shouldFilterAllDispatcherTypes(false)
.requestMatchers(AUTH_WHITELIST).permitAll()
.requestMatchers(RESOURCE_LIST).permitAll()
.requestMatchers(ADMIN_PAGE_LIST).hasRole("ADMIN")
.anyRequest().authenticated()
.and().addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider),
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ssu/eatssu/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public static User oAuthJoin(@NotNull String email, @NotNull OauthProvider provi
return new User(email, Role.USER, provider, providerId, UserStatus.ACTIVE, credentials);
}

/**
* <--Static Factory Method-->
* admin 회원가입
* Role 은 다른 방법으로 세팅할 예정
*/
public static User adminJoin(@NotNull String loginId, @NotNull String credentials) {
return new User(loginId, Role.USER, OauthProvider.EATSSU, loginId, UserStatus.INACTIVE, credentials);
}

public void updateNickname(@NotNull String nickname) {
this.nickname = nickname;
}
Expand Down

0 comments on commit 0a6341a

Please sign in to comment.