스프링/스프링 시큐리티

세션 설정

까마귀! 2024. 3. 23. 22:37

 

MainController
@Controller
public class MainController {

    @GetMapping("/")
    public String main(Model model){

        // 세션 사용 id
        String id = SecurityContextHolder.getContext().getAuthentication().getName();

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Iterator<? extends GrantedAuthority> iter = authorities.iterator();
        GrantedAuthority auth = iter.next();
        String role = auth.getAuthority(); // 검증된 값이 맞는지 활용 등등, 여러가지로 활용 가능


        model.addAttribute("id", id);
        model.addAttribute("role",role);

        return "main";
    }
}

 

 

 

 

main.mustache
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
main page
<hr>
{{id}}
{{role}}
</body>
</html>

 

 

 

 

실행

 

 

 

 

로그인 뒤에

 

잘 실행이 되는걸 확인할 수 있다

 

 

 

 

세션 소멸 시간 설정

application.properties

# session time
server.servlet.session.timeout=1800 # 초단위

# session time
server.servlet.session.timeout=90m # 분 단위

 

 

중복 로그인 방지

SecurityConfig
http.
        sessionManagement((auth) -> auth    // 다중로그인 설정
                .maximumSessions(1)   // 동시 접속 중복을 최대로 허용하는 로그인수
                .maxSessionsPreventsLogin(true)); // 다중 로그인 개수를 초과하였을 경우 초과시 새로운 로그인 차단

 

 

고정보호

SecurityConfig
http
        .sessionManagement((auth) -> auth
                .sessionFixation().changeSessionId()); // 세션 쿠키를 탈취 당했을시 세션 아이디를 바꿈

'스프링 > 스프링 시큐리티' 카테고리의 다른 글

InMemory  (0) 2024.03.23
csrf 적용  (0) 2024.03.23
로그인 검증 로직  (0) 2024.03.23
회원가입 로직  (0) 2024.03.23
BCrypt 암호화 메소드 기본설정  (0) 2024.03.07