Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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/2/spring/13.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
Java 在Spring中使用autowiredbean对表达式进行预授权_Java_Spring_Spring Security - Fatal编程技术网

Java 在Spring中使用autowiredbean对表达式进行预授权

Java 在Spring中使用autowiredbean对表达式进行预授权,java,spring,spring-security,Java,Spring,Spring Security,我的Spring应用程序中的资源有以下类 @RestController @RequestMapping("/whatever") public class SomeResource { @Autowired private CoolService coolService; @RequestMapping( path = "", method = RequestMethod.GET) @PreAuthorize("hasPerm(@co

我的Spring应用程序中的资源有以下类

@RestController
@RequestMapping("/whatever")
public class SomeResource {

@Autowired
private CoolService coolService;

@RequestMapping(
            path = "",
            method = RequestMethod.GET)
    @PreAuthorize("hasPerm(@coolService.resolve(#attribute))")
    public void resource(@PathVariable("attribute") int attribute) {
        ...
    }
我想调用实现由Spring上下文自动连接的CoolService的bean,因为对于CoolService,我有两个bean,它们根据启动时的配置文件被激活

public interface CoolService {

    resolve(int attribute);
}
然而,Spring似乎不知道该使用哪个bean,因为没有一个bean只命名为CoolService,而且在预授权中,我无法编写@SupercolService或@ultraCoolService,因为它依赖于配置文件


如何实现这一点?

如果您想定义两个bean来实现相同的接口,那么您可以使用user annotation@Qualifier。 例如:

@Service
@Qualifier("service1")
public interface SuperCoolService implements CoolService {

    public Object resolve(int attribute){...}
}

@Service
@Qualifier("service1")
public interface UltraCoolService implements CoolService {

    public Object resolve(int attribute){...}
}

请注意,@coolService在Spring上下文中引用bean名称,而不是服务类上的字段。是的,我想通过使用@coolService我指的是这个,但我认为如果没有找到具有这个显式名称的bean,Spring可以解析这个bean。或者至少有另一种方法可以引用自动连线bean?但是我找不到任何东西…您可以使用权限计算器和自定义表达式来定义自定义授权。查看:指定相同的名称!在@Service注释中。在某个配置文件中,只有一个配置文件是活动的。请阅读我的评论。。。我建议将@ServicecoolService添加到您的实现中。但这并不能解决我的问题,因为在预授权语句中,我必须将@service1或@service2放在不同的位置,这取决于我要使用的位置,这意味着更改代码、构建和重新编译所有内容,我想让Spring根据配置文件来解决这些问题。
@Service
@Profile("ultra")
public interface UltraCoolService implements CoolService {

    public Object resolve(int attribute){...}
}
@Service
@Qualifier("service1")
public interface SuperCoolService implements CoolService {

    public Object resolve(int attribute){...}
}

@Service
@Qualifier("service1")
public interface UltraCoolService implements CoolService {

    public Object resolve(int attribute){...}
}