您可以在在源代码中使用注释来直接为 Java™ EE 企业应用程序提供安全性。
使用安全性注释
@Stateless
@RolesAllowed("team")
public class TestEJB implements Test {
@PermitAll
public String hello(String msg) {
return "Hello, " + msg;
}
public String goodbye(String msg) {
return "Goodbye, " + msg;
}
}
在本示例中,hello() 方法可供任何人访问,而 goodbye() 方法则供角色小组的用户访问。
组件 | 用于检查角色的 API |
---|---|
EJB | javax.ejb.EJBContext.isCallerInRole(role) |
servlet | javax.servlet.http.HttpServletRequest.isUserInRole(role) |
Web Service | javax.xml.ws.WebServiceContext.isUserInRole(role) |
@Stateless
@DeclaresRoles({"A", "B"})
public class TestEJB implements Test {
@Resource private SessionContext sc;
public String hello(String msg) {
if (sc.isCallerInRole("A") && !sc.isCallerInRole("B")) {
...
} else {
...
}
}
}
安全性注释的无效使用
@PermitAll
@DenyAll
public String test()
@RolesAllowed("team")
@RolesAllowed("otherteam")
public String hello()
@RolesAllowed({"team", "otherteam"})
public String hello()