@Secured 在控制器中不起作用,但intercept-url 似乎工作正常 [英] @Secured does not work in controller, but intercept-url seems to be working fine

查看:14
本文介绍了@Secured 在控制器中不起作用,但intercept-url 似乎工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来我的@Controller 中的方法上的@Secured 没有被读取.当使用基于 sec:intercept-url 的安全过滤时,这似乎工作得很好.以下代码导致 Spring Security 给我这个日志条目:

It doesn't look like @Secured on methods in my @Controller are being read. When security filtering based on sec:intercept-url is being used, this seems to be working just fine. The following code results in Spring Security giving me this log entry:

调试:org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 公共对象 - 未尝试身份验证

DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Public object - authentication not attempted

web.xml

上下文配置位置/WEB-INF/spring/root-context.xml

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 和所有编组的配置.此配置是注释驱动的.

servlet-context.xml holds the configuration of the viewResolvers and all the marshalling. This configuration is annotation-driven.

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() {
    }

}

这似乎与我使用的身份验证方法没有任何关系,因此可以忽略 basic-http-tag.

This doesn't seem to have any relation to which authentication method I'm using, so the basic-http-tag can be overlooked.

我有一个想法,@Secured 不起作用,因为它在另一个上下文中使用,而不是在配置安全性的 root-context.xml 中使用.我已经尝试将此配置移动到 servlet-context.xml,但它似乎没有到达 springSecurityFilterChain.对这个问题和我的理论有什么想法吗?

I have this idea that the @Secured doesn't work because of it being used in another context than the root-context.xml in which the security is being configured. I've tried to move this configuration to the servlet-context.xml, but it doesn't seem to reach the springSecurityFilterChain. Any thoughts on the problem and and my theory?

推荐答案

你说得对, 是在每个上下文的基础上应用的.但是,您不需要将整个安全配置移动到 servlet-context.xml,只需向其中添加一个 元素即可.

You are right, <global-method-security> is applied at per-context basis. However, you don't need to move the whole security configuration to servlet-context.xml, just add a <global-method-security> element to it.

这篇关于@Secured 在控制器中不起作用,但intercept-url 似乎工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆