Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/38 add enable global method security #71

Merged
merged 3 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/main/java/com/undertheriver/sgsg/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.undertheriver.sgsg.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -9,6 +10,7 @@
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
Expand All @@ -20,6 +22,11 @@
@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final CustomOAuth2UserService customOAuth2UserService;
Expand All @@ -34,6 +41,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final AuthenticationEntryPoint authenticationEntryPoint;

private final AccessDeniedHandler customAccessDeniedHandler;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
Expand Down Expand Up @@ -61,10 +70,11 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(customAccessDeniedHandler)
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어떤 PR이었는지 기억은 안나지만 이 녀석의 선언 순서가 변경되었던 적이 있었던 것 같은데요.
혹시 이 녀석 선언 순서도 동작에 영향을 미칠까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

명확하진 않지만
.anyRequest().authenticated() 하단에 작성하면 무관할 것 같습니다

.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.undertheriver.sgsg.config.security.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class RestAccessDeniedHandler implements AccessDeniedHandler {

private final HandlerExceptionResolver handlerExceptionResolver;

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) {

handlerExceptionResolver.resolveException(request, response, null, accessDeniedException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.undertheriver.sgsg.config.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.web.servlet.HandlerExceptionResolver;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {

private final HandlerExceptionResolver handlerExceptionResolver;

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {

handlerExceptionResolver.resolveException(request, response, null, authException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Slf4j
@Configuration
@RequiredArgsConstructor
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
public class RestAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

private final HttpCookieOAuth2AuthorizationRequestRepository authorizationRequestRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@

@Configuration
@Slf4j
public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
private final JwtProvider jwtProvider;
private final Set<URI> authorizedRedirectUris;

public CustomAuthenticationSuccessHandler(
public RestAuthenticationSuccessHandler(
HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository,
JwtProvider jwtProvider,
AppProperties appProperties
Expand Down