Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @后授权不起作用_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Spring @后授权不起作用

Spring @后授权不起作用,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我试图构建一个简单的处理程序方法,防止用户浏览属于不同用户的项目。方法如下: @PostAuthorize("principal.username == #model['username']") @RequestMapping(value = "/show/{id}", method = RequestMethod.GET) public String single(@PathVariable Long id, Model model) { Item item = itemServic

我试图构建一个简单的处理程序方法,防止用户浏览属于不同用户的项目。方法如下:

@PostAuthorize("principal.username == #model['username']")
@RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
public String single(@PathVariable Long id, Model model)  {
    Item item = itemService.findById(id);
    model.addAttribute("item", item);
    model.addAttribute("username", item.getUser().getUsername());
    return "item";
}
因此,主要思想是将principal.username与存储在模型中的用户名进行比较。我使用的是Spring5.0.5和security 5.0.4。无引导的Java配置。我的配置保持不变(amnogst其他内容)


尽管如此,我还是能够登录,而不是通过不同用户的直接url访问项目。欢迎任何提示。谢谢

将此方法安全配置添加到您的项目中。此配置作为全局配置,当然您的
proxyTargetClass=true
,因此spring也可以为您的控制器类生成代理

import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    /*
        We can enable annotation-based security using the @EnableGlobalMethodSecurity annotation
        on any @Configuration instance. 

    */

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        return new OAuth2MethodSecurityExpressionHandler();
    }
}

希望这能解决您的问题。

将此方法安全配置添加到您的项目中。此配置作为全局配置,当然您的
proxyTargetClass=true
,因此spring也可以为您的控制器类生成代理

import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    /*
        We can enable annotation-based security using the @EnableGlobalMethodSecurity annotation
        on any @Configuration instance. 

    */

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        return new OAuth2MethodSecurityExpressionHandler();
    }
}

希望这能解决您的问题。

好的,以防其他人遇到同样的问题。问题是我在控制器上使用了
@Pre/@PostAuthorize
注释。控制器通常不在接口后面,默认情况下,配置行为的方面没有启动

我的解决方案是在扫描包以搜索控制器的
WebConfig
类上启用
@EnableAspectJAutoProxy

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"rs.saga.web"})
public class WebConfig implements WebMvcConfigurer {
   ...
}

好的,以防其他人遇到同样的问题。问题是我在控制器上使用了
@Pre/@PostAuthorize
注释。控制器通常不在接口后面,默认情况下,配置行为的方面没有启动

我的解决方案是在扫描包以搜索控制器的
WebConfig
类上启用
@EnableAspectJAutoProxy

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"rs.saga.web"})
public class WebConfig implements WebMvcConfigurer {
   ...
}

谢谢ataur,这绝对是个好建议,proxyTargetClass绝对有意义。然而,它仍然不起作用。你有什么建议,如果注释被考虑的话,如何调试。顺便说一下,我添加了一个DefaultMethodSecurityExpressionHandler,从现在开始使用您的配置您的
@ComponentScan(basePackages={“com.*})
值是多少?在安全配置类上,我没有任何配置。您在项目中的任何地方配置了它吗?当然,我的意思是,由于自定义用户详细信息服务位于服务所在的包中,因此将拾取该服务。如果没有更多的IDE,我将尝试将一个项目精简到最少,并发布一个github链接感谢ataur,这绝对是一个好建议,proxyTargetClass绝对有意义。然而,它仍然不起作用。你有什么建议,如果注释被考虑的话,如何调试。顺便说一下,我添加了一个DefaultMethodSecurityExpressionHandler,从现在开始使用您的配置您的
@ComponentScan(basePackages={“com.*})
值是多少?在安全配置类上,我没有任何配置。您在项目中的任何地方配置了它吗?当然,我的意思是,由于自定义用户详细信息服务位于服务所在的包中,因此将拾取该服务。如果没有更多的IDE,我将尝试将一个项目剥离到最低限度,并发布一个github链接