-keystore keystore.p12 : key store 이름을 keystore.p12로 지정한다.
위와 같이 값을 입력 후 생성이 끝나면 keystore 파일이 프로젝트 디렉토리 안에 생성된다.
application.yml 혹은 application.properties에 다음과 같이 설정한다.
---
# local 환경
spring:
profiles: local
server:
ssl:
enabled: true
key-store: keystore.p12 # 프로젝트 디렉토리 내부에 있기 때문에 절대 경로를 지정하지 않음
key-store-password: <비번>
key-store-type: PKCS12
key-alias: bns-ssl
port: 8081
설정 후 아래와 같이 https로 요청하고 인증서라는 경고문은 아래와 같이 처리하면 인증서가 적용된 사이트를 볼 수 있다.
https://localhost:8081
Spring Security를 이용하여 사용자 정보 찾는 방법
SpringContextHolder를 사용하는 방법
Authenticationauthentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println(authentication.getName());
Useruser = (User) authentication.getPrincipal(); //현재 세션 사용자의 객체 가져오기//UserDetails를 구현한 사용자 객체가 가지고 있는 정보들을 리턴Collection<? extendsGrantedAuthority> authorities = authentication.getAuthorities();
booleanroleComfirm = authorities.stream().filter(o -> o.getAuthority().equals("ROLE_ADMIN")).findAny().isPresent();
//현재 사용자가 ROLE_ADMIN이라는 ROLE을 가지고 있는 지 확인