use*_*337 5 java spring spring-security
我无法理解为什么我的安全性无法正常工作.方法hasPermission()在Evaluator类中甚至没有被调用.我认为我的安全配置有问题.
我的安全配置:
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns="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.xsd">
<sec:http use-expressions="true">
<sec:intercept-url pattern="/favicon.ico" access="permitAll"/>
<sec:intercept-url pattern="/resources/**" access="permitAll"/>
<sec:intercept-url pattern="/login" access="permitAll"/>
<sec:form-login login-page="/login"
username-parameter="login"
password-parameter="password"
authentication-failure-url="/login?error"
authentication-success-handler-ref="successHandler"/>
<sec:logout logout-url="/logout" logout-success-url="/login?logout"/>
<sec:access-denied-handler error-page="/WEB-INF/views/error/403.jsp"/>
</sec:http>
<sec:authentication-manager>
<sec:authentication-provider ref="userAuthenticationProvider"/>
</sec:authentication-manager>
<sec:global-method-security pre-post-annotations="enabled" secured-annotations="enabled">
<sec:expression-handler ref="expressionHandler"/>
</sec:global-method-security>
<bean:bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<bean:property name="permissionEvaluator" ref="permissionEvaluator"/>
</bean:bean>
<bean:bean id="permissionEvaluator" class="de.mark.project.security.UserPermissionEvaluator">
<bean:constructor-arg ref="userSecurityService"/>
</bean:bean>
<bean:bean id="successHandler" class="de.mark.project.security.UrlAuthenticationSuccessHandler"/>
<bean:bean id="userSecurityService" class="de.mark.project.service.UserService"/>
<bean:bean name="userAuthenticationProvider" class="de.mark.project.security.UserAuthenticationProvider">
<bean:constructor-arg ref="userSecurityService"/>
</bean:bean>
</bean:beans>
Run Code Online (Sandbox Code Playgroud)
这是我的自定义评估者:
public class UserPermissionEvaluator implements PermissionEvaluator {
private UserService service;
@Autowired
public UserPermissionEvaluator(UserService service) {
this.service = service;
}
@Override
public boolean hasPermission(Authentication authentication,
Serializable targetId, String targetType,
Object permission) {
UserDetails principal = (UserDetails) authentication.getPrincipal();
User authorizedUser = service.getUser(principal.getUsername());
Collection<Permission> userPermissions = authorizedUser.getPermissions();
for (Permission p : userPermissions) {
if (p.getName().equals(allowedPermission)) {
return true;
}
}
return false;
}
@Override
public boolean hasPermission(Authentication authentication,
Serializable targetId, String targetType,
Object permission) {
throw new RuntimeException("Error");
}
}
Run Code Online (Sandbox Code Playgroud)
使用安全性的控制器中的方法:
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasPermission(null, 'create_user')")
public String createUserForm(@ModelAttribute("user") User user) {
return "user/internal/form/credentialForm";
}
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决它?
UPD:
<web-app
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_5.xsd">
<!-- log4j configuration -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Config Setup -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-config/security.xml
classpath:/spring-config/data.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-config/mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Error handling -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
</web-app>
Run Code Online (Sandbox Code Playgroud)
UPD 2
Spring MVC配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<ctx:component-scan base-package="de.mark.project.web"/>
<sec:global-method-security pre-post-annotations="enabled"/>
<mvc:resources mapping="/resources/**" location="classpath:/style/"/>
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
这可能是因为该<global-method-security>标签需要与您的 Spring MVC 配置处于相同的上下文中,否则您的控制器将不会进行后期处理。这在常见问题解答中进行了讨论。
例如,如果您的 web.xml 如下所示:
<!--
- Location of the XML file that defines the root application context
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
<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>
<!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc/*.xml</param-value>
</init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)
为了支持控制器上的方法安全性,请确保<global-method-security>在 /WEB-INF/mvc/*.xml 中的位置定义标签。请注意,配置的其余部分应保留在原来的位置。如果您想支持服务的方法安全性,您可能还需要<global-method-security>父级(即现在可能所在的位置)。
如果这没有帮助,请发布您的 web.xml 或 WebApplicationInitializer(如果您不使用 web.xml)
| 归档时间: |
|
| 查看次数: |
4958 次 |
| 最近记录: |