복붙노트

[SPRING] @ 보안은 컨트롤러에서 작동하지 않지만 요격은 정상적으로 작동하는 것 같습니다.

SPRING

@ 보안은 컨트롤러에서 작동하지 않지만 요격은 정상적으로 작동하는 것 같습니다.

@Controller에있는 메소드에 대한 @Secured가 읽히지 않습니다. sec : intercept-url을 기반으로하는 보안 필터링이 사용되는 경우 이는 정상적으로 작동하는 것 같습니다. 다음 코드는 Spring Security에서이 로그 항목을 제공합니다.

을 포함한다.

contextConfigLocation                      /WEB-INF/spring/root-context.xml

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Filter security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

servlet-context.xml은 viewResolvers 및 모든 마샬링의 구성을 보유합니다. 이 구성은 주석으로 구동됩니다.

root-context.xml

    <sec:global-method-security secured-annotations="enabled" />

<sec:http auto-config="true">
    <sec:http-basic/>
</sec:http>

<!-- Declare an authentication-manager to use a custom userDetailsService -->
<sec:authentication-manager>
    <sec:authentication-provider
        user-service-ref="userDetailsService">
        <sec:password-encoder ref="passwordEncoder" />
    </sec:authentication-provider>
</sec:authentication-manager>

<bean
    class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder"
    id="passwordEncoder" />
<sec:user-service id="userDetailsService">
    <sec:user name="john" password="john" authorities="ROLE_USER, ROLE_ADMIN" />
    <sec:user name="jane" password="jane" authorities="ROLE_USER" />
</sec:user-service>

PingController.java

@Controller
public class PingController {

    @Secured("ROLE_ADMIN")
    @RequestMapping(value = "/ping", method = RequestMethod.GET)
    public void ping() {
    }

}

이것은 내가 사용하고있는 인증 방법과 관련이없는 것처럼 보이므로 기본 http 태그를 간과 할 수 있습니다.

@Secured가 보안이 구성되는 root-context.xml이 아닌 다른 컨텍스트에서 사용되기 때문에 작동하지 않는다는 생각이 들었습니다. 이 구성을 servlet-context.xml로 이동하려고 시도했지만 springSecurityFilterChain에 도달하지 못하는 것 같습니다. 문제에 대한 생각과 내 이론?

해결법

  1. ==============================

    1.맞습니다. 는 컨텍스트 단위로 적용됩니다. 그러나 전체 보안 구성을 servlet-context.xml로 이동하지 않고 요소 만 추가하면됩니다.

    맞습니다. 는 컨텍스트 단위로 적용됩니다. 그러나 전체 보안 구성을 servlet-context.xml로 이동하지 않고 요소 만 추가하면됩니다.

  2. ==============================

    2.스프링 보안 FAQ (강조 표시)를 참조하십시오. pointcuts를 서비스 레이어에 적용하면 앱의 보안 컨텍스트에서 만 설정하면됩니다.

    스프링 보안 FAQ (강조 표시)를 참조하십시오. pointcuts를 서비스 레이어에 적용하면 앱의 보안 컨텍스트에서 만 설정하면됩니다.

  3. from https://stackoverflow.com/questions/6651119/secured-does-not-work-in-controller-but-intercept-url-seems-to-be-working-fine by cc-by-sa and MIT license