Framework & Library/Spring Security
-
Spring Security - Authentication 객체가 가질 수 있는 2가지 타입Framework & Library/Spring Security 2021. 10. 3. 15:51
Authentication 객체가 가질 수 있는 2가지 타입 ㆍ 로그인 진행이 완료되면 기존의 Sesson 공간에 Security Session 공간이 만들어진다. ㆍ Security Session에 들어갈 수 있는 정보는 Authentication 객체여야 한다. ㆍ 또한 Authentication 객체가 가질 수 있는 타입은 UserDetails와 OAuth2User 2가지 타입이다. ㆍ UserDetails와 OAuth2User 2가지 타입을 통해서 로그인을 한 사용자의 정보를 얻을 수 있다. 일반 로그인 사용자 정보 받기 IndexController 클래스 수정 @GetMapping("/test/login") @ResponseBody /* 일반 로그인 사용자에 대한 정보 받기 */ public S..
-
Spring Security - 구글 로그인 사용자 정보 받기Framework & Library/Spring Security 2021. 10. 2. 17:34
구글 로그인 후처리 PrincipalOauth2UserService 클래스 생성 @Service public class PrincipalOauth2UserService extends DefaultOAuth2UserService { @Override public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { return super.loadUser(userRequest); } } ㆍ config 패키지에 oauth 패키지를 생성한 후 패키지 내에 PrincipalOauth2UserService 클래스를 위 코드와 같이 생성한다. ㆍ 해당 클래스는 소셜 로그인에 성공한 후 후속조치를 수행하는 OA..
-
Spring Security - 구글 로그인 준비Framework & Library/Spring Security 2021. 10. 2. 16:16
API 설정 프로젝트 생성 ㆍ 구글 API Console 사이트에 접속을 한 후 위 사진에 표시된 버튼을 클릭한 후 spring-oauth-google이란 프로젝트를 생성한다. OAuth 동의 화면 설정 ㆍ 프로젝트를 생성한 후 위 사진에 표시된 버튼을 클릭한 후 spring-oauth라는 이름으로 OAuth 동의 화면을 설정한다. 사용자 인증 정보 설정 ㆍ 위 사진에 표시된 버튼을 클릭한 후 spring-oauth라는 이름과 "http://localhost:8080/login/oauth2/code/google"의 URI로 사용자 인증 정보를 설정한다. ㆍ "http://localhost:8080/login/oauth2/code/google"는 OAuth Client 라이브러리를 사용하게 되면 고정으로 ..
-
Spring Security - 권한 처리Framework & Library/Spring Security 2021. 10. 1. 16:19
애너테이션을 통한 권한 처리 SpringConfig 클래스 수정 @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) ㆍ SpringConfig 클래스에 위 애너테이션을 추가한다. ㆍ 위 애너테이션은 @Secured와 @PreAuthorize 애너테이션을 활성화해주는 역할을 한다. ㆍ 스프링 시큐리티에서는 @Secured와 @PreAuthorize 애너테이션을 통한 권한 처리가 가능한다. IndexController 클래스 수정 @Secured("ROLE_ADMIN") @GetMapping("/info") @ResponseBody public String info() { return "개인정보"; } ㆍ IndexControl..
-
Spring Security - 로그인Framework & Library/Spring Security 2021. 10. 1. 15:06
로그인 설정 SecurityConfig 클래스 수정 @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeRequests() .antMatchers("/user/**").authenticated() // 인증 필요 .antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')") // ROLE_ADMIN, ROLE_MANAGER 권한 필요 .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") // ROLE_ADMIN 권한 필요..
-
Spring Security - 회원가입Framework & Library/Spring Security 2021. 9. 30. 16:21
User 객체 생성 @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String username; private String password; private String email; private String role; private String provide; private String providerId; @CreationTimestamp private Timestamp createDate; } ㆍ 위 코드는 회원가입을 하는 사용자의 정보를 받기 위한 User 객체이다. ㆍ User 객체는 id, 사용자 이름, 패스워드 등의 정보를 담고 있다. 회..