You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
5.1 KiB
121 lines
5.1 KiB
|
11 months ago
|
package kr.re.etri.autoflow.security;
|
||
|
11 months ago
|
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.context.annotation.Bean;
|
||
|
|
import org.springframework.context.annotation.Configuration;
|
||
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
||
|
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||
|
|
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||
|
|
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
||
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||
|
|
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||
|
|
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||
|
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||
|
|
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.authentication.UsernamePasswordAuthenticationFilter;
|
||
|
|
|
||
|
11 months ago
|
import kr.re.etri.autoflow.security.jwt.AuthEntryPointJwt;
|
||
|
|
import kr.re.etri.autoflow.security.jwt.AuthTokenFilter;
|
||
|
|
import kr.re.etri.autoflow.security.services.UserDetailsServiceImpl;
|
||
|
11 months ago
|
|
||
|
|
@Configuration
|
||
|
|
//@EnableWebSecurity
|
||
|
|
@EnableMethodSecurity
|
||
|
|
//(securedEnabled = true,
|
||
|
|
//jsr250Enabled = true,
|
||
|
|
//prePostEnabled = true) // by default
|
||
|
|
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
|
||
|
|
@Autowired
|
||
|
|
UserDetailsServiceImpl userDetailsService;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private AuthEntryPointJwt unauthorizedHandler;
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public AuthTokenFilter authenticationJwtTokenFilter() {
|
||
|
|
return new AuthTokenFilter();
|
||
|
|
}
|
||
|
|
|
||
|
|
// @Override
|
||
|
|
// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
|
||
|
|
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
|
||
|
|
// }
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public DaoAuthenticationProvider authenticationProvider() {
|
||
|
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
||
|
|
|
||
|
|
authProvider.setUserDetailsService(userDetailsService);
|
||
|
|
authProvider.setPasswordEncoder(passwordEncoder());
|
||
|
|
|
||
|
|
return authProvider;
|
||
|
|
}
|
||
|
|
|
||
|
|
// @Bean
|
||
|
|
// @Override
|
||
|
|
// public AuthenticationManager authenticationManagerBean() throws Exception {
|
||
|
|
// return super.authenticationManagerBean();
|
||
|
|
// }
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
|
||
|
|
return authConfig.getAuthenticationManager();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Bean
|
||
|
|
public PasswordEncoder passwordEncoder() {
|
||
|
|
return new BCryptPasswordEncoder();
|
||
|
|
}
|
||
|
|
|
||
|
|
// @Override
|
||
|
|
// protected void configure(HttpSecurity http) throws Exception {
|
||
|
|
// http.cors().and().csrf().disable()
|
||
|
|
// .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
|
||
|
|
// .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
|
||
|
|
// .authorizeRequests().antMatchers("/api/auth/**").permitAll()
|
||
|
|
// .antMatchers("/api/test/**").permitAll()
|
||
|
|
// .anyRequest().authenticated();
|
||
|
|
//
|
||
|
|
// http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
||
|
|
// }
|
||
|
|
|
||
|
11 months ago
|
// @Bean
|
||
|
|
// public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||
|
|
// http.csrf(AbstractHttpConfigurer::disable)
|
||
|
|
// .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
|
||
|
|
// .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||
|
|
// .authorizeHttpRequests(auth ->
|
||
|
|
// auth.requestMatchers("/api/auth/**").permitAll()
|
||
|
|
// .requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
|
||
|
|
// .requestMatchers("/api/test/**").permitAll()
|
||
|
|
// .anyRequest().authenticated()
|
||
|
|
// );
|
||
|
|
//
|
||
|
|
// http.authenticationProvider(authenticationProvider());
|
||
|
|
//
|
||
|
|
// http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
||
|
|
//
|
||
|
|
// return http.build();
|
||
|
|
// }
|
||
|
|
|
||
|
|
// 임시 설정
|
||
|
11 months ago
|
@Bean
|
||
|
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||
|
|
http.csrf(AbstractHttpConfigurer::disable)
|
||
|
11 months ago
|
.exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
|
||
|
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||
|
|
.authorizeHttpRequests(auth ->
|
||
|
|
auth.anyRequest().permitAll() // 모든 요청 허용
|
||
|
|
);
|
||
|
11 months ago
|
|
||
|
11 months ago
|
http.authenticationProvider(authenticationProvider());
|
||
|
11 months ago
|
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
||
|
11 months ago
|
|
||
|
11 months ago
|
return http.build();
|
||
|
|
}
|
||
|
|
}
|