gpt4 book ai didi

java - 为什么我的自定义 PermissionEvaluator 不起作用?

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:25 24 4
gpt4 key购买 nike

我不明白为什么我的安全不能正常工作。 Evaluator 类中的方法 hasPermission() 甚至没有被调用。我认为我的安全配置有问题。

我的安全配置:

<?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>

这是我的自定义评估器:

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");
}
}

Controller 中使用安全性的方法:

@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasPermission(null, 'create_user')")
public String createUserForm(@ModelAttribute("user") User user) {
return "user/internal/form/credentialForm";
}

有什么解决办法吗?

更新:

<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>

更新 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>

最佳答案

这可能是因为 <global-method-security>标记需要与您的 Spring MVC 配置处于相同的上下文中,否则您的 Controller 将不会被后处理。这是 discussed in the FAQ .

例如,如果您的 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>

要在 Controller 上支持方法安全性,请确保 <global-method-security>标记在/WEB-INF/mvc/*.xml 中的某个位置定义。请注意,配置的其余部分应保留在原处。如果你想在你的服务上支持方法安全,你可能还需要 <global-method-security>在父级中(即它现在可能所在的位置)。

如果这没有帮助,如果您没有使用 web.xml,请发布您的 web.xml 或 WebApplicationInitializer

关于java - 为什么我的自定义 PermissionEvaluator 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com