• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

SecurityConfig의 rememberMe 질문입니다.

22.05.11 00:30 작성 조회수 134

0

의존성 주입을 받음에도 불구하고 Remember me on this computer. 체크박스를 클릭하고 로그인시 아래와 같은 에러가 발생합니다. 

SecurityConfig의 코드는 아래와 같습니다. @Autowired도 해봤지만 결과가 같고, 체크박스 표시를 하지 않으면 로그인이 됩니다. 뭐가 문제인지 잘 모르겠습니다 ㅠㅠ

package spring.security;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final UserDetailsService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
// 인가
http.authorizeRequests()
.anyRequest()
.authenticated();

// 인증
// FormLogin 인증 방식 api
http.formLogin()
.defaultSuccessUrl("/")
.failureUrl("/login")
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
System.out.println("authentication" + authentication.getName());
response.sendRedirect("/");
}
})
.failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
System.out.println("exception" + exception.getMessage());
response.sendRedirect("/login");
}
})
.and()
.logout()
.logoutSuccessUrl("/login")
.addLogoutHandler(new LogoutHandler() {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
HttpSession session = request.getSession();
session.invalidate();;
}
})
.logoutSuccessHandler(new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.sendRedirect("/login");
}
})
.and()
.rememberMe()
.tokenValiditySeconds(3600) // default : 14
.userDetailsService(userDetailsService());
}
}

답변 1

답변을 작성해보세요.

0

내용만 봐서는 문제될 것은 없어 보이는데요..

소스 공유 해 주시면 좀 더 정확한 원인을  파악하는데 도움이 될 것 같습니다