xan*_*oss 6 java restful-authentication jax-rs
我正在尝试保护JAX-RS端点,目前正在尝试弄清楚身份验证和授权是如何工作的.大多数示例都非常简单,因为它们仅通过web.xml从Java EE App-Server角色中捎带.
我想知道如何使用除Java EE AS角色以外的其他东西.例如:我想使用会话或某种令牌(或某种标识符).
这一切都取决于您正在使用的JAX-RS实现.我在嵌入式Jetty上使用Jersey.
SecurityHandler sh = new SecurityHandler();
// the UserRealm is the collection of users, and a mechanism to determine if
// provided credentials are valid
sh.setUserRealm(new MyUserRealm());
// the Authenticator is a strategy for extracting authentication credentials
// from the request. BasicAuthenticator uses HTTP Basic Auth
sh.setAuthenticator(new BasicAuthenticator());
Run Code Online (Sandbox Code Playgroud)
一旦你有Principal在HttpServletRequest,你可以注入到这些的JAX-RS请求的上下文.
public abstract class AbstractResource {
private Principal principal;
@Context
public void setSecurityContext(SecurityContext context) {
principal = context.getUserPrincipal();
}
protected Principal getPrincipal() {
return principal;
}
}
@Path("/some/path")
public class MyResource extends AbstractResource {
@GET
public Object get() {
Principal user = this.getPrincipal();
// etc
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3373 次 |
| 最近记录: |