这可能是一个很长的帖子,所以我道歉但我无法破解这个。我已经设法在测试Spring MVC项目上设置Spring Security,这样基本的http身份验证工作正常。我的下一步是在服务上设置方法身份验证,因此我遵循了几个教程。不幸的是,我的方法不受保护,因为我可以像任何授权用户一样访问我的测试方法,无论角色如何。我应该提到我并不是想要保护控制器方法,因为我知道需要AspectJ,我还没准备好。如果他们有帮助,这是我的配置文件。谢谢!
部分log4j输出:
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre- instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@23ec1e48: defining beans [helloController,homeController,testService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.security.web.PortMapperImpl#0,org.springframework.security.web.context.HttpSessionSecurityContextRepository#0,org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0,org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0,org.springframework.security.authentication.AnonymousAuthenticationProvider#0,org.springframework.security.web.savedrequest.HttpSessionRequestCache#0,org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint#0,org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0,org.springframework.security.config.http.UserDetailsServiceInjectionBeanPostProcessor#0,org.springframework.security.filterChainProxy,org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler#0,org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0,org.springframework.security.access.vote.AffirmativeBased#1,org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor#0,org.springframework.security.methodSecurityMetadataSourceAdvisor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.security.core.userdetails.memory.InMemoryDaoImpl#0,org.springframework.security.authentication.dao.DaoAuthenticationProvider#0,org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0,org.springframework.security.authenticationManager]; root of factory hierarchy
DEBUG: org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource - @org.springframework.security.access.prepost.PreAuthorize(value=hasRole('ROLE_ADMIN')) found on specific method: public java.lang.String com.arturoaraya.testapp.HelloController.printAdminWelcome(org.springframework.ui.ModelMap)
DEBUG: org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource - Adding security method [CacheKey[com.arturoaraya.testapp.HelloController; public java.lang.String com.arturoaraya.testapp.HelloController.printAdminWelcome(org.springframework.ui.ModelMap)]] with attributes [[authorize: 'hasRole('ROLE_ADMIN')', filter: 'null', filterTarget: 'null']]
INFO : org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor - Validated configuration attributes
DEBUG: org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource - @org.springframework.security.access.prepost.PreAuthorize(value=hasRole('ROLE_USER')) found on specific method: public java.lang.String com.arturoaraya.testapp.HelloController.printWelcome(org.springframework.ui.ModelMap)
DEBUG: org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource - Adding security method [CacheKey[com.arturoaraya.testapp.HelloController; public java.lang.String com.arturoaraya.testapp.HelloController.printWelcome(org.springframework.ui.ModelMap)]] with attributes [[authorize: 'hasRole('ROLE_USER')', filter: 'null', filterTarget: 'null']]
DEBUG: org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource - Added URL pattern: /welcome*; attributes: [ROLE_USER, ROLE_ADMIN]
的web.xml
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>Spring MVC Application</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<!-- Spring 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>
弹簧security.xml文件
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.3.xsd">
<http auto-config="true">
<intercept-url pattern="/welcome*" access="ROLE_USER,ROLE_ADMIN" />
<logout invalidate-session="true" logout-success-url="/welcome" />
</http>
<global-method-security pre-post-annotations="enabled" />
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="123456" authorities="ROLE_USER" />
<user name="admin" password="password" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
MVC-调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="com.arturoaraya.testapp" />
<context:spring-configured/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
更新:
以下是受保护的方法定义:
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String getAdminMessage() {
return "Welcome admin user!<br>Here is your secret message:<br><br>BOO!";
}