gpt4 book ai didi

java - @PreAuthorize 无效。可能是什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:15 25 4
gpt4 key购买 nike

有一个管理页面,我在其中使用了 @PreAuthorize 角色 ROLE_ADMIN 注释,但是没有此角色的用户也可以访问此页面。

这个安全上下文文件

<?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"
xmlns:p="http://www.springframework.org/schema/p"
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
pre-post-annotations="enabled" secured-annotations="enabled" />

<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/app/login"
access="permitAll" />

<security:intercept-url pattern="/app/admin"
access="hasRole('ROLE_USER') and over18" />

<security:intercept-url pattern="/app/**"
access="hasRole('ROLE_USER')" />

<security:form-login login-page="/app/login"
default-target-url="/app/base/" always-use-default-target="true"
authentication-failure-url="/app/login?error=true" />

<security:logout delete-cookies="JSESSIONID"
logout-success-url="/app/logout" />

<security:access-denied-handler
error-page="/app/User/error/403" />

<security:remember-me key="webapp-key" />
<security:expression-handler ref="expressionHandler" />
</security:http>


<bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService"></property>
</bean>

<bean class="org.springframework.security.authentication.ProviderManager">
<constructor-arg>
<list>
<ref bean="daoAuthenticationProvider" />
</list>
</constructor-arg>
</bean>


<security:authentication-manager>
<security:authentication-provider
user-service-ref="userDetailsService">
<security:password-encoder hash="md5"></security:password-encoder>
</security:authentication-provider>
</security:authentication-manager>

</beans>

Controller 是:

@Controller
@RequestMapping("/admin")
public class AdminController {

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping
public String showAdminPage(){
return "adminPage";
}
}

编辑:

flag

我添加了代码来检查用户和授予的权限,然后将它们打印出来。这是输出

*************************************** [ROLE_USER] uddeshya *************************************** for 

System.out.println("***************************************\n\n\n");
System.out.println(SecurityContextHolder.getContext()
.getAuthentication().getAuthorities());
System.out.println(principal.getName()); System.out.println("\n
\n\n***************************************");

最佳答案

要在 Spring MVC Controller 中使用基于注解的方法安全性,您需要在 DispatcherServlet 的上下文配置文件中设置以下内容。

<security:global-method-security
pre-post-annotations="enabled" secured-annotations="enabled" />

看来你把它放在了不同的配置文件中。

关于java - @PreAuthorize 无效。可能是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156407/

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