ABOUT ME

-

오늘 방문자
-
어제 방문자
-
전체
-
  • [Spring Security] ajax요청시 403 에러 발생 처리방법
    Spring 2018. 11. 18. 23:55

    Spring Security에서 ajax로 요청을 하게되면 403 에러가 발생한다.


    발생한 원인은 CSRF처리를 해주지 않아서 이다.


    여기서 처리한 방법은 [특정 requestMethod를 모두 허용해주는 방식]으로 처리하였다.   (※ CSRF를 사용하지 않음.)


    Spring Security 버전마다 약간 씩 처리 방법은 다르다


    버전은 3.2.3 사용하였다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!-- Spring Security -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>3.2.3.RELEASE</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>3.2.3.RELEASE</version>
            </dependency>
    cs

    4.0이상에서는 csrf 속성에 disabled라는 속성이있어서 true, false값만 주면 자동으로 쉽게 처리 할 수 있다.
    (spring 버전과 호환성 체크가 꼭필요하다. 나같은 경우는 시큐리티버전4.0이상으로 올리니까 에러남..)





    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package domean.security.hendler;
     
    import java.util.regex.Pattern;
     
    import javax.servlet.http.HttpServletRequest;
     
    import org.springframework.security.web.util.matcher.RegexRequestMatcher;
    import org.springframework.security.web.util.matcher.RequestMatcher;
     
    /**
     * 스프링시큐리티 CSRF 처리 클래스 입니다. 
     * @author CM
     *
     */
    public class CsrfSecurityRequestMatcher implements RequestMatcher{
        
        private Pattern allowRequestMethod = Pattern.compile("^(GET|POST|DELETE|UPDATE|PATCH)$");
        private RegexRequestMatcher unprotectedMatcher  = new RegexRequestMatcher("/accessdenied"null);
        
        
        @Override
        public boolean matches(HttpServletRequest request) {
            
            if(allowRequestMethod.matcher(request.getMethod()).matches()) {
                return false;
            }
            return !unprotectedMatcher.matches(request);
        
        }
     
    }//CLASS END
     
    cs





    security-config.xml


    37행, 43행에 추가해줬다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    <http auto-config="true" use-expressions="true" > 
            <intercept-url pattern="/newmember/insert"  access="isAnonymous()" />
            <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')"/> 
            <intercept-url pattern="/member/**" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" /> 
            <intercept-url pattern="/downloadFile" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" /> 
            <intercept-url pattern="/downloadPhoto" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" /> 
            <intercept-url pattern="/index"  access="isAnonymous()" />
            <intercept-url pattern="/login"  access="isAnonymous()" /> 
            <intercept-url pattern="/facebooklogin"  access="permitAll()" />    
     
             <form-login
                      login-processing-url="/logincheck"
                     username-parameter="memberId"
                     password-parameter="memberPw"
                     login-page="/index"
                     authentication-success-handler-ref="LoginSuccessHandler"
                     authentication-failure-handler-ref="LoginFailureHandler"
            />
            <logout
                     logout-url="/logout"
                     logout-success-url="/index"
                     invalidate-session="true"
              />
                  
                <!-- 세션이 끊겼을때 이동할 페이지 -->
                <session-management invalid-session-url="/index"> 
                  <!-- 1번 접속자 유지 -->
                   <!-- <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> -->
                   
                   <!-- 중복 로그인 일어 났을 시 이동 할 주소 -->
                   <concurrency-control max-sessions="1" expired-url="/index" /> 
                </session-management>
                
                <!-- 권한없음 강제이동페이지 -->
                <access-denied-handler error-page="/accessdenied"/>
                
                <csrf request-matcher-ref="CsrfSecurityRequestMatcher"/>
                
               <!--  <custom-filter after="FORM_LOGIN_FILTER"  ref="FacebookAuthenticationFilter" /> -->  
               
    </http>
     
    <beans:bean id="CsrfSecurityRequestMatcher" class="domean.security.hendler.CsrfSecurityRequestMatcher"/>
    cs


    댓글

Designed by Tistory.