3 Commits

Author SHA1 Message Date
hou27
b54514acab Fix code a bit 2022-06-09 19:00:28 +09:00
hou27
06a5352fb9 Remove old code 2022-06-02 22:38:50 +09:00
hou27
e28750154e Apply Security 5.7.1 2022-06-02 22:00:10 +09:00
3 changed files with 20 additions and 39 deletions

View File

@@ -49,7 +49,8 @@ dependencies {
/*
Security
*/
implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot-starter-security:2.6.7'
implementation 'org.springframework.boot:spring-boot-starter-security:2.7.0'
/*
Validation

View File

@@ -1,5 +1,6 @@
package demo.api.config;
import demo.api.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -10,46 +11,22 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
/**
* Spring Security 사용을 위한 Configuration Class를 작성하기 위해서
* WebSecurityConfigurerAdapter를 상속하여 클래스를 생성하고
* @Configuration 애노테이션 대신 @EnableWebSecurity 애노테이션을 추가한다.
*/
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
/**
* PasswordEncoder를 Bean으로 등록
*/
public class SecurityConfig {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
public UserDetailsService userDetailsService() {
return new UserDetailsServiceImpl();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// /**
// * 인증 or 인가가 필요 없는 경로를 설정
// */
// @Override
// public void configure(WebSecurity web) throws Exception {
// web.ignoring().antMatchers("/?/**");
// }
/**
* 인증에 대한 지원
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
/**
* 인증 or 인가에 대한 설정
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin()
@@ -63,5 +40,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests()
.antMatchers("/", "/user/signUp", "/user/userList", "/user/signIn*").permitAll()
.anyRequest().authenticated();
return http.build();
}
}

View File

@@ -8,21 +8,22 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
private final UserRepository userRepository;
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String email) throws UserNotFoundException {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new UserNotFoundException());
.orElseThrow(UserNotFoundException::new);
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
return new org