【问题标题】:Spring Security - @Secured works only in mvc ControllerSpring Security - @Secured 仅适用于 mvc 控制器
【发布时间】:2011-03-09 03:01:14
【问题描述】:


我正在使用 Spring Security + MVC。
注释 @Secured({ "ROLE_ADMIN" }) 只能在控制器层中正常工作。
如果我尝试在更深/其他层中使用它,我不会收到安全错误。
或者,如果我尝试在“无 mvc 映射”方法上使用它,则不会出现安全错误。
遵循我的 xml 配置文件:
web.xml:

  <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring-security.xml
        /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j-myapp.properties</param-value>
    </context-param>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/Management/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

spring-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Declare a view resolver -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
     <context:component-scan base-package="com.affiliates" />

</beans>

spring-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-3.0.xsd
            http://www.springframework.org/schema/security 

            http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    <security:global-method-security secured-annotations="enabled" />
    <security:http auto-config="true" use-expressions="true"
        access-denied-page="/Management/auth/denied">

        <security:intercept-url pattern="/Management/auth/login"
            access="permitAll" />
        <security:intercept-url pattern="/Management/main/admin"
            access="hasRole('ROLE_EMPLOYEE')" />
        <security:intercept-url pattern="/Management/api/affiliates/**"
            access="hasRole('ROLE_EMPLOYEE')" />

        <security:form-login login-page="/Management/auth/login/"
            authentication-failure-url="/Management/auth/login?error=true"
            login-processing-url="/Management/auth/j_spring_security_check"
            default-target-url="/Management/auth/login?error=false" />
        <security:logout invalidate-session="true"
            logout-success-url="/Management/auth/login/" logout-url="/Management/auth/logout" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider
            user-service-ref="customUserDetailsService">
            <security:password-encoder ref="passwordEncoder" />
        </security:authentication-provider>
    </security:authentication-manager>

    <bean
        class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"
        id="passwordEncoder" />
    <bean id="customUserDetailsService" class="com.affiliates.service.CustomUserDetailsService" />
</beans>

mvc-dispacher-servlet.xml

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.affiliates.controllers" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

applocationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />

    <!-- Scans the classpath for annotated components that will be auto-registered 
        as Spring beans. For example @Controller and @Service. Make sure to set the 
        correct base-package -->
    <context:component-scan base-package="com.affiliates" />

    <!-- Configures the annotation-driven Spring MVC Controller programming 
        model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
    <mvc:annotation-driven />

</beans>

我是这样使用它的: 安全方法:

 @Component
    public  class BrandsApi{
    @Secured({ "ROLE_ADMIN" })
        public ResultContainer getAll() {
            return brandDao.getAll(getSecurityFilter().getBrandSecurityFilter());
        }
    }
}

调用者:

@Controller
@RequestMapping("/api/brands")
public class BrandsController {
@RequestMapping(value = "/get")
    public ModelAndView get(){
       BrandsApi brandsApi = new BrandsApi();
       brandsApi.getAll();
}
}

这是我的最新更新
你好,
我已将我的配置转换为运行正常的 javaconfig 文件。
我在加载时调试我的应用程序,我看到参数已被传输。
表示brandApi已初始化
代码:

@Configuration
public class SpringJavaConfig { 
    @Bean
    public BrandsApi brandsApi(){
        return new BrandsApi();
    }
}

在 BrandsApi 中,我有一个上面带有 @Secured({ "ROLE_ADMIN" }) 的方法

这就是我调用该方法的方式: 代码:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
    BrandsApi brandsApi = (BrandsApi)ctx.getBean(BrandsApi.class);
        brandsApi.getAll();

但由于某种原因,即使我已经登录了 ROLE_EMPLOYEE,我也可以进去
这是我的 BrandsApi 课程:
代码:

class BrandsApi extends BaseApi{
    @Secured({ "ROLE_ADMIN" })
    public void getAll() {
        System.out.println("Hello");
    }
}

【问题讨论】:

  • 请提供任何创建“更深/其他层”的示例。
  • @Stas Kurilin - 我编辑我的帖子,谢谢

标签: java spring-mvc spring-security


【解决方案1】:

只有当实例是由 Spring 创建时,注解才有效。您必须将要使用它的每个类转换为 bean 并将其注册到应用程序上下文中。

另请注意,如果您进行内部调用,注释将被忽略:

 Foo foo = context.getBean( "foo", Foo.class );
 foo.foo(); // <-- annotatoon works here

但如果foo() 调用this.foo2(),则不再有检查。所以foo2() 的任何注释都会被忽略。

【讨论】:

  • 谢谢,你能告诉我我需要在哪个 xml 中注册 bean。小例子会有很大帮助,谢谢
  • 您需要将它们添加到applocationContext.xml,或者您需要将正确的注释添加到类(因为您使用context:component-scan)。在这种情况下,我认为应该是@Component。我建议创建一个小测试用例以确保它按您的意愿工作。
  • 我没有例子;这是凭记忆 :-)
  • @Aaron Digulla - 我已经尝试过了,但它不起作用......我编辑我的帖子
  • 你不能打电话给new。请改用context.getBean( BrandsApi.class )
猜你喜欢
  • 1970-01-01
  • 2016-10-18
  • 2015-01-28
  • 2017-04-05
  • 2011-01-23
  • 2013-07-17
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多