我正在尝试做这样的事情(有效)
@Service("myService")
@Scope("session")
public class MyService {
public boolean hasAccessOnResource(String str)
{
return true;
}
@PreAuthorize("@myService.hasAccessOnResource(#str)")
public void execute(String str)
{
System.out.println("hello");
}
}
然而,当MyService实现一个接口并且我通过接口调用execute时,我得到一个异常。
在控制器(用于参考)
@Controller
@Scope("session")
public class HomeController {
@Autowired
@Qualifier("myService")
MyServiceInterface service; // <- doesn't work case
//MyService service; <- working case
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(HttpSession session)
{
service.execute("hello");
return "home";
}
}
实现接口的服务..
@Service("myService")
@Scope("session")
public class MyService implements MyServiceInterface{
public boolean hasAccessOnResource(String str)
{
return true;
}
@PreAuthorize("@myService.hasAccessOnResource(#str)")
@Override
public void execute(String str)
{
System.out.println("hello");
}
}
例外:
org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 11): Method call: Method hasAccessOnResource(java.lang.String) cannot be found on com.sun.proxy.$Proxy52 type
我做错了什么?这是正确的方法吗?如果我想对此接口方法进行PreAuthorize,我需要采取什么方法?