在Controller方法上使用@Secured注释时,Spring安全性基于JDK的代理问题

时间:2016-03-08 06:23:44

标签: java spring security spring-mvc spring-security

我正在做一些RnD来学习Spring Security.While使用方法级安全性我试过以下:

控制器界面

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

    @RequestMapping("/admin")
    public interface AdminCtrl {

        @RequestMapping(value = { "/get" }, method = { RequestMethod.GET })
        public @ResponseBody
        String getSomething();
    }

控制器Impl类

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/admin")
@Controller
public class AdminCtrlImpl implements AdminCtrl {

    @Override
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @Secured(value = "ROLE_ADMIN")
    public @ResponseBody
    String getSomething() {

        return SecurityContextHolder.getContext().getAuthentication().getName()
                + "==> Responding with HI";
    }

}

弹簧security.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <security:global-method-security
        secured-annotations="enabled" />
    <security:http>
        <security:form-login />
    </security:http>
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="alpha" authorities="ROLE_ADMIN"
                    password="password" />
                <security:user name="beta" authorities="ROLE_USER"
                    password="password" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

应用-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.alpha.sample" />
</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
    <display-name>SpringSecurity</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

但是当我访问http://localhost:8080/SpringMethodLevelSecurity/admin/get时。它始终使用匿名用户登录并始终显示:

anonymousUser ==&GT;以HI回复

为什么它没有显示任何身份验证机制屏幕,例如form-login或http login

__

P.S。虽然我知道安全注释主要属于服务层。但我想知道上面指定的情况。

由于

1 个答案:

答案 0 :(得分:2)

你应该插入

<security:intercept-url pattern="/admin/get" access="ROLE_ADMIN"/>
<{1>}标记下的

如下:

<security:http>

关键是您的系统已准备好执行安全检查,但您没有指定Spring Security必须应用安全检查的url模式。

实际上@Secured注释执行安全检查但不直接在Web上下文中,但在“应用程序上下文”中,在Web上下文中更正确的方法是配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <security:global-method-security secured-annotations="enabled" /> <security:http> <security:form-login /> <security:intercept-url pattern="/admin/get" access="ROLE_ADMIN"/> </security:http> <security:authentication-manager> <security:authentication-provider> <security:user-service> <security:user name="alpha" authorities="ROLE_ADMIN" password="password" /> <security:user name="beta" authorities="ROLE_USER" password="password" /> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans> 部分配置,换句话说,您的配置不起作用,因为您配置的过滤器以<security:http>...</security:http>配置为基础 我希望这可以帮到你