*모든 내용은 책에 있는 내용을 기반으로 작성하였습니다.
5장 ----
스프링 시큐리티(Spring Security)는 막강한 인증과 인가 기능을 가진 프레임워크이다.
로그인을 직접 구현해야 하는 경우 배보다 배꼽이 더 커지는 경우가 많아서 소셜 로그인을 사용하는 경우가 많아지고 있다.
로그인시 직접 구현해야 하는것
- 로그인시 보안
- 비밀번호 찾기
- 회원가입 시 이메일 혹은 전화번호 인증
- 비밀번호 변경
- 회원정보 변경
대부분 2.0을 많이 사용하며
spring-security-oauth2-autoconfigure
를 사용함으로서 스프링부트 2.0에서도 스프링부트 1.5에서 쓰던 설정을 그대로 사용할 수 있다.스프링부트 1.5 방식에서는 url 주소를 모두 명시해야하지만 2.0방식에서는 client 인증 정보만 입력하면 된다.
책 보면서 진행
package com.jojoldu.book.springboot.domain.user;
import com.jojoldu.book.springboot.domain.BaseTimeEntity;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Getter
@NoArgsConstructor
@Entity
public class User extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String email;
@Column
private String picture;
@Enumerated(EnumType.STRING) //(1)
@Column(nullable = false)
private Role role;
@Builder
public User(String name, String email, String picture,Role role){
this.name = name;
this.email = email;
this.picture = picture;
this.role = role;
}
public User update(String name, String picture){
this.name= name;
this.picture = picture;
return this;
}
public String getRoleKey(){
return this.role.getKey();
}
}
package com.jojoldu.book.springboot.domain.user;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum Role {
GUEST("ROLE_GUEST","손님"),
USER("ROLE_USER","일반 사용자");
private final String key;
private final String title;
}
스프링 시큐리티에서는 권한 코드에는 항상 ROLE_이 앞에 있어야만 한다. 그래서 코드별 키 값을 ROLE_GUEST, ROLE_USER 등으로 지정한다.
package com.jojoldu.book.springboot.config.auth;
import com.jojoldu.book.springboot.domain.user.Role;
import lombok.RequiredArgsConstructor;
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;
@RequiredArgsConstructor
@EnableWebSecurity //(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final CustomOAuth2UserService customOAuth2UserService;
@Override
protected void configure(HttpSecurity http) throws Exception{
http.csrf().disable().headers().frameOptions().disable() //(2)
.and()
.authorizeRequests() //(3)
.antMatchers("/", "/css/**", "/images/**", "/js/**" , "/h2-console/**").permitAll()
.antMatchers("/api/v1/**").hasRole(Role.USER.name()) //(4)
.anyRequest().authenticated() //(5)
.and().logout().logoutSuccessUrl("/") //(6)
.and()
.oauth2Login() //(7)
.userInfoEndpoint() //(8)
.userService(customOAuth2UserService); //(9)
}
}
package com.jojoldu.book.springboot.config.auth;
import com.jojoldu.book.springboot.config.auth.dto.OAuthAttributes;
import com.jojoldu.book.springboot.config.auth.dto.SessionUser;
import com.jojoldu.book.springboot.domain.user.User;
import com.jojoldu.book.springboot.domain.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.Collections;
@RequiredArgsConstructor
@Service
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
private final UserRepository userRepository;
private final HttpSession httpSession;
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2UserService delegate = new DefaultOAuth2UserService();
OAuth2User oAuth2User = delegate.loadUser(userRequest);
String registrationId = userRequest.getClientRegistration().getRegistrationId(); //(1)
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails()
.getUserInfoEndpoint().getUserNameAttributeName(); //(2)
OAuthAttributes attributes = OAuthAttributes.of(registrationId, userNameAttributeName, oAuth2User.getAttributes()); //(3)
User user = saveOrUpdate(attributes);
httpSession.setAttribute("user", new SessionUser(user)); //(4)
return new DefaultOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(user.getRoleKey())),
attributes.getAttributes(),
attributes.getNameAttributeKey());
}
private User saveOrUpdate(OAuthAttributes attributes) {
User user = userRepository.findByEmail(attributes.getEmail())
.map(entity -> entity.update(attributes.getName(), attributes.getPicture()))
.orElse(attributes.toEntity());
return userRepository.save(user);
}
}
package com.jojoldu.book.springboot.config.auth.dto;
import com.jojoldu.book.springboot.domain.user.Role;
import com.jojoldu.book.springboot.domain.user.User;
import lombok.Builder;
import lombok.Getter;
import java.util.Map;
@Getter
public class OAuthAttributes {
private Map<String, Object> attributes;
private String nameAttributeKey;
private String name;
private String email;
private String picture;
@Builder
public OAuthAttributes(Map<String,Object> attributes, String nameAttributeKey, String name, String email, String picture){
this.attributes = attributes;
this.nameAttributeKey = nameAttributeKey;
this.name = name;
this.email = email;
this.picture = picture;
}
//(1)
public static OAuthAttributes of(String registrationId, String userNameAttributeName, Map<String,Object> attributes){
return ofGoogle(userNameAttributeName, attributes);
}
public static OAuthAttributes ofGoogle(String userNameAttributeName, Map<String,Object> attributes){
return OAuthAttributes.builder()
.name((String) attributes.get("name"))
.email((String) attributes.get("email"))
.picture((String) attributes.get("picture"))
.attributes(attributes)
.nameAttributeKey(userNameAttributeName)
.build();
}
//(2)
public User toEntity(){
return User.builder().name(name)
.email(email)
.picture(picture)
.role(Role.GUEST)
.build();
}
}
package com.jojoldu.book.springboot.config.auth.dto;
import com.jojoldu.book.springboot.domain.user.User;
import lombok.Getter;
import java.io.Serializable;
@Getter
public class SessionUser implements Serializable {
private String name;
private String email;
private String picture;
public SessionUser(User user){
this.name = user.getName();
this.email = user.getEmail();
this.picture = user.getPicture();
}
}
인증된 사용자 정보만 필요하다.
User와 다르게 직렬화를 구현하였는데, 직렬화를 구현하지 않았으면 에러가 발생하기 때문이다.
그렇다면 굳이 User 에 직렬화를 넣지 않고 새로 만든 이유는
User는 Entity 이기 때문이다.
Entity 클래스에는 언제 다른 엔티티와 관계가 형성될지 모르기 때문에, 만약에 자식 엔티티를 가지고 있다면 직렬화 대상에 자식들도 포함되니 성능 이슈, 부수효과가 발생할 수 있기 때문이다.
{{>layout/header}}
<h1>스프링부트로 시작하는 웹 서비스 Ver.2</h1>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<a href="/posts/save" role="button" class="btn btn-primary">글 등록</a>
{{#userName}} //(1)
Logged in as: <span id="user">{{userName}}</span>
<a href="/logout" class="btn btn-info active" role="button">Logout</a> //(2)
{{/userName}}
{{^userName}} //(3)
<a href="/oauth2/authorization/google" class="btn btn-success active" role="button">Google Login</a> //(4)
<a href="/oauth2/authorization/naver" class="btn btn-secondary active" role="button">Naver Login</a>
{{/userName}}
</div>
</div>
<br>
<!-- 목록 출력 영역 -->
<table class="table table-horizontal table-bordered">
<thead class="thead-strong">
<tr>
<th>게시글번호</th>
<th>제목</th>
<th>작성자</th>
<th>최종수정일</th>
</tr>
</thead>
<tbody id="tbody">
{{#posts}}
<tr>
<td>{{id}}</td>
<td><a href="/posts/update/{{id}}">{{title}}</a></td>
<td>{{author}}</td>
<td>{{modifiedDate}}</td>
</tr>
{{/posts}}
</tbody>
</table>
</div>
{{>layout/footer}}
public class IndexController {
private final PostsService postsService;
private final HttpSession httpSession;
@GetMapping("/")
public String index(Model model){
model.addAttribute("posts",postsService.findAllDesc());
SessionUser user = (SessionUser) httpSession.getAttribute("user"); //(1)
if(user!=null){ //(2)
model.addAttribute("userName",user.getName());
}
return "index";
}
...
package com.jojoldu.book.springboot.config.auth;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER) //(1)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginUser { //(2)
}
package com.jojoldu.book.springboot.config.auth;
import com.jojoldu.book.springboot.config.auth.dto.SessionUser;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.http.HttpSession;
@RequiredArgsConstructor
@Component
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver {
private final HttpSession httpSession;
@Override
public boolean supportsParameter(MethodParameter parameter) {//(1)
boolean isLoginUserAnnotation = parameter.getParameterAnnotation(LoginUser.class) !=null;
boolean isUserClass = SessionUser.class.equals(parameter.getParameterType());
return isLoginUserAnnotation && isUserClass;
}
@Override //(2)
public Object resolveArgument(
MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
return httpSession.getAttribute("user");
}
}
@GetMapping("/")
public String index(Model model, @LoginUser SessionUser user){ //(1)
model.addAttribute("posts",postsService.findAllDesc());
if(user!=null){
model.addAttribute("userName",user.getName());
}
return "index";
}
기존에 httpSession.getAttribute("user")로 가져오던 세션 정보 값이 개선되었다.
이제는 어느 컨트롤러든지 @LoginUser만 사용하면 세션 정보를 가져올수 있다.
세션은 기본적으로 WAS의 메모리에서 저장되고 호출된다. 메모리에 저장되다 보니 내장 톰캣처럼 애플리케이션 실행 시 실행되는 구조에서는 항상 초기화가 된다.
또한 2대 이상의 서버에서 서비스하 고 있다면 톰캣마다 동기화 설정을 해야한다.
그래서 실제 서비스에서는 세션 저장소에 대해서 다음의 3가지 중 한가지를 선택한다.
- 톰캣 세션을 사용한다.
- 일반적으로 별 다른 설정을 하지 않을 때 기본적으로 선택되는 방식이다.
- 이렇게 될 경우 톰캣에 세션이 저장되기 때문에 2대 이상의 WAS가 구동되는 환경에서는 톰캣들 간의 세션 공유를 위한 추가 설정이 필요합니다.
- MySQL과 같은 데이터베이스를 세션 저장소로 사용한다.
- 여러 WAS 간의 공용 세션을 사용할 수 있는 가장 쉬운 방법이다.
- 많은 설정이 필요 없지만, 결국 로그인 요청마다 DB IO가 발생하여 성능상 이슈가 발생할 수 있다.
- 보통 로그인 요청이 많이 없는 백오피스, 사내 시스템 용도에서 사용한다.
- Redis, Memcached와 같은 메모리 DB를 세션 저장소로 사용한다.
- B2C 서비스에서 가장 많이 사용하는 방식이다.
- 실제 서비스로 사용하기 위해서는 Embedded Redis와 같은 방식이 아닌 외부 메모리 서버가 필요하다.
user_name의 이름을 네이버에서는 response 해야한다.
네이버의 회원 조회시 반화되는 json 형태 때문이다.
스프링 시큐리티에선 하위 필드를 명시할 수 없다.
최상위 필드들만 user_name으로 지정 가능하다.하지만 네이버 응답값 최상위 필드는 resultCode,message,response이다.
그래서 스프링 시큐리티에서 인식가능한 필드는 위 3개중에 골라야하므로 repsonse를 user_name으로 지정하고 이후 response의 id를 user_name으로 한다.
시큐리티를 적용하게 되면 기존 태스트에서 문제가 발생할 수 있다.
인증에 대한 권한을 받지 못한 상태이므로, 테스트 코드마다 인증한 사용자가 호출한것처럼 작동해야한다.