출처
이분 때문에 살았다
너무 깔금하고 이해하기 쉽게 글을써놓셨다
package com.example.demo2.Config;
import com.example.demo2.Admin.Repo.AdminRepo;
import com.example.demo2.Admin.Service.AdminPrincipalDetailsService;
import com.example.demo2.User.Repo.UserRepo;
import com.example.demo2.User.Service.UserPrincipalDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration//빈등록: 스프링 컨테이너에서 객체에서 관리
@EnableWebSecurity/////필터를 추가해준다
@RequiredArgsConstructor
public class SecurityConfig {
private final AdminRepo adminRepo;
private final UserRepo userRepo;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@Order(1)
public SecurityFilterChain adminConfigure(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.securityMatcher(new AntPathRequestMatcher("/admin/**"))
.authenticationProvider(adminDaoAuthenticationProvider())
.authorizeHttpRequests(auth->
auth.requestMatchers("/admin/**").hasRole("ADMIN")
)
.formLogin(login ->
login
.loginPage("/admin/login")
.loginProcessingUrl("/admin/login/pro")
.permitAll()
.passwordParameter("pwd")
.defaultSuccessUrl("/admin/index", true)
.failureUrl("/admin/login?error=true")
)
.logout(logout ->
logout
.logoutUrl("/admin/logout")
.permitAll()
);
return http.build();
}
@Bean
@Order(2)
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.authenticationProvider(userDaoAuthenticationProvider())
.authorizeHttpRequests(auth->
auth. requestMatchers("/auth/**").authenticated() // /auth/** URL에 대해 인증이 필요하도록 설정
.anyRequest().permitAll())
.formLogin(login ->
login
.loginProcessingUrl("/login/pro")
.permitAll()
.passwordParameter("pwd")
.defaultSuccessUrl("/index", true)
.failureUrl("/login?error=true")
)
.logout(logout ->
logout
.logoutUrl("/logout")
.permitAll()
);
return http.build();
}
@Bean
DaoAuthenticationProvider adminDaoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(new AdminPrincipalDetailsService(adminRepo));
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}
@Bean
DaoAuthenticationProvider userDaoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(new UserPrincipalDetailsService(userRepo));
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}
}
'Springboot > Security' 카테고리의 다른 글
Springboot Security 2.7.+ 에서 authenticationManagerBean (0) | 2022.07.07 |
---|---|
Springboot Security 2.7. + 버전 WebSecurityConfigurerAdapter (0) | 2022.07.07 |