반응형
Spring Security : API용 JWT 토큰 및 웹용 세션
Spring Boot 앱에서 두 가지 보안을 모두 사용하는 것을 목표로 합니다.JWT로 API 쪽은 이미 했는데, WEB 쪽은 어떻게 세션을 구현해야 할지 모르겠습니다.저는 이미 다른 프로젝트에서 그것을 했지만 어떻게 하면 그들이 함께 일할 수 있는지 모르겠습니다.
여기 내꺼SecurityConfig
:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/api/**")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/register").permitAll()
.antMatchers("/api/login").permitAll()
.antMatchers("/api/public").permitAll()
.antMatchers("/api/lost").permitAll()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/contact").permitAll()
.antMatchers("/resources/**").permitAll()
.antMatchers("/file/**").permitAll()
.anyRequest().authenticated()
.and()
.apply(new JWTConfigurer(this.tokenProvider));
}
저는 다음과 같은 것을 갖고 싶습니다:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// For API side something like : .match("/api/**")
// No CSRF
.csrf().ignoringAntMatchers("/api/**")
// STATELESS session
// Use token filter
.apply(new JWTConfigurer(this.tokenProvider));
// For WEB side something like : .match "others"
// Use CSRF
.csrf()
// Use session
// And the other permit :
.authorizeRequests()
.antMatchers("/api/register").permitAll()
.antMatchers("/api/login").permitAll()
.antMatchers("/api/public").permitAll()
.antMatchers("/api/lost").permitAll()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/contact").permitAll()
.antMatchers("/resources/**").permitAll()
.antMatchers("/file/**").permitAll()
.anyRequest().authenticated();
}
그것을 어떻게 하는지 누가 알려줄 수 있습니까? (그리고 어떻게 작동하는지 설명해주세요.)저는 제가 요청하는 것에 대해 어떤 좋은 해결책도 찾지 못했습니다.
6시간 동안 검색한 후 해결책은 다음과 같습니다. https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/ #다중 검색 보안
편집: 다음은 제가 수행한 방법입니다.
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12);
}
@Configuration
@Order(1)
public class ApiSecurityAdapter extends WebSecurityConfigurerAdapter {
private TokenProvider tokenProvider;
public ApiSecurityAdapter(TokenProvider tokenProvider) {
this.tokenProvider = tokenProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**") //<= Security only available for /api/**
.authorizeRequests()
.antMatchers("/api/register").permitAll()
.antMatchers("/api/login").permitAll()
.antMatchers("/api/public").permitAll()
.antMatchers("/api/lost").permitAll()
.anyRequest().authenticated()
.and()
.apply(new JWTConfigurer(this.tokenProvider))
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
@Configuration
public class WebSecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http // <= Security available for others (not /api/)
.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/central", false)
.failureForwardUrl("/login/fail")
.and()
.logout()
.invalidateHttpSession(true)
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.and()
.csrf();
}
}
}
이것이 도움이 되기를 바랍니다!
언급URL : https://stackoverflow.com/questions/44970848/spring-security-jwt-token-for-api-and-session-for-web
반응형
'it-source' 카테고리의 다른 글
git 명령에 대한 PowerShell의 별칭을 생성하시겠습니까? (0) | 2023.07.30 |
---|---|
powershell에서 문자열을 정의하기 위한 단일 따옴표와 이중 따옴표의 차이점은 무엇입니까? (0) | 2023.07.25 |
현재 노드 버전 확인 (0) | 2023.07.25 |
PHP: 배열을 값의 길이로 정렬하시겠습니까? (0) | 2023.07.25 |
HTML 페이지에 .png 이미지를 내장할 수 있습니까? (0) | 2023.07.25 |