gpt4 book ai didi

java - @PreAuthorize 注释不起作用 Spring 安全性

转载 作者:IT老高 更新时间:2023-10-28 13:47:17 25 4
gpt4 key购买 nike

我发现了很多类似的问题,但没有一个能解决我的问题。我的问题是ROLE_USER可以访问ROLE_ADMIN的功能

我的 spring-security.xml 代码如下。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:s="http://www.springframework.org/schema/security"
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.xsd">

<s:http auto-config="true" use-expressions="true">
<s:intercept-url pattern="/index.jsp" access="permitAll" />
<s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" />
<s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" />

<s:form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<s:logout logout-success-url="/logout" />
</s:http>

<s:authentication-manager>
<s:authentication-provider>
<s:user-service>
<s:user name="asif" password="123456" authorities="ROLE_USER,ROLE_ADMIN" />
<s:user name="raheel" password="123456" authorities="ROLE_USER" />
</s:user-service>
</s:authentication-provider>
</s:authentication-manager>

当我添加 <s:global-method-security pre-post-annotations="enabled"/>我的代码显示资源未找到错误,当我删除我的代码时执行成功,但 ROLE_USER 可以访问 ROLE_ADMIN 函数

我的 Controller 功能是。

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value="/delete", method = RequestMethod.GET)
public String DeleteAll(ModelMap model, Principal principal ) {

org.springframework.security.core.userdetails.User activeUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
System.out.println("Active user is "+activeUser.getUsername()+"Authorities are "+activeUser.getAuthorities());
return "deleteUsers";

}

最佳答案

如果您使用的是 XML 配置,请不要忘记添加以下属性:

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

如果您使用的是 Java Configuration,请不要忘记添加以下注解:

@EnableGlobalMethodSecurity(prePostEnabled = true)

关于java - @PreAuthorize 注释不起作用 Spring 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11841156/

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