Tob*_*now 9 java jax-rs jaas java-ee-6 jboss7.x
Java Security是我过去几周的主要话题,我归档了以下内容:
extends AuthenticatorBase)extends UsernamePasswordLoginModule)的自定义登录模块我的主要问题是,我的端点只能使用注释@DeclareRoles,如果我不使用它,我就无法通过身份验证.详细地说,该方法AuthenticatorBase.invoke(来自org.apache.catalina.authenticator)调用该方法,RealmBase.hasResourcePermission然后检查角色.
由于我不使用任何预定义角色,因此检查将失败.
我的问题:有没有办法使用这样的代码:
@Path("/secure")
@Stateless
public class SecuredRestEndpoint {
@Resource
SessionContext ctx;
@GET
public Response performLogging() {
// Receive user information
Principal callerPrincipal = ctx.getCallerPrincipal();
String userId = callerPrincipal.getName();
if (ctx.isCallerInRole("ADMIN")) {
// return 200 if ok
return Response.status(Status.OK).entity(userId).build();
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
一些额外的背景:只要用户名被转发(X-FORWARD-USER),就需要使用反向代理进行身份验证.这就是为什么我使用自己的Authenticator类和自定义登录模块(我没有任何密码凭据).但我认为应用服务器本身的标准身份验证方法也会出现问题
由于您的代码是静态的(意味着您拥有可以保护的静态资源集),因此除了增加访问粒度之外,我不了解您添加安全角色的要求.我可以在模块化环境中看到此要求,其中代码不是静态的,在这种情况下,您需要支持后续部署声明的其他安全角色.
也就是说,我必须实现类似的东西,一个支持以下的安全系统:
我将在高层次的抽象中描述我所做的事情,并希望它会给你一些有用的想法.
首先,实现一个@EJB这样的东西:
@Singleton
@LocalBean
public class MySecurityDataManager {
public void declareRole(String roleName) {
...
}
public void removeRole(String roleName) {
...
}
/**
* Here, I do not know what your incoming user data looks like such as:
* do they have groups, attributes? In my case I could determine user's groups
* and then assign them to roles based on those. You may have some sort of
* other attribute or just plain username to security role association.
*/
public void associate(Object userAttribute, String roleName) {
...
}
public void disassociate(Object userAttribute, String roleName) {
...
}
/**
* Here basically you inspect whatever persistence method you chose and examine
* your existing associations to build a set of assigned security roles for a
* user based on the given attribute(s).
*/
public Set<String> determineSecurityRoles(Object userAttribute) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
然后实现自定义javax.security.auth.spi.LoginModule.我建议从头开始实现它,除非你知道提供的容器抽象实现对你有用,但它不适合我.另外,如果你不熟悉,我建议你熟悉以下内容,以便更好地理解我的目标:
public class MyLoginModule implements LoginModule {
private MySecurityDataManager srm;
@Override
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options) {
// make sure to save subject, callbackHandler, etc.
try {
InitialContext ctx = new InitialContext();
this.srm = (MySecurityDataManager) ctx.lookup("java:global/${your specific module names go here}/MySecurityDataManager");
} catch (NamingException e) {
// error logic
}
}
@Override
public boolean login() throws LoginException {
// authenticate your user, see links above
}
@Override
public boolean commit() throws LoginException {
// here is where user roles get assigned to the subject
Object userAttribute = yourLogicMethod();
Set<String> roles = srm.determineSecurityRoles(userAttribute);
// implement this, it's easy, just make sure to include proper equals() and hashCode(), or just use the Jboss provided implementation.
Group rolesGroup = new SimpleGroup("Roles", roles);
// assuming you saved the subject
this.subject.getPrincipals().add(rolesGroup);
}
@Override
public boolean abort() throws LoginException {
// see links above
}
@Override
public boolean logout() throws LoginException {
// see links above
}
}
Run Code Online (Sandbox Code Playgroud)
为了允许动态配置(即声明角色,关联用户),构建一个UI,使用相同的@EJB MySecurityDataManager来CRUD您的安全设置,登录模块将使用它来确定安全角色.
现在,您可以按照您想要的方式打包它们,只需确保MyLoginModule可以查找MySecurityDataManager并将它们部署到容器中.我在JBoss工作,你提到了JBoss,所以这对你也有用.更强大的实现将包括LoginModule配置中的查找字符串,然后您可以在运行时从方法中的选项映射中读取该字符串initialize().这是JBoss的示例配置:
<security-domain name="mydomain" cache-type="default">
<authentication>
<login-module flag="required"
code="my.package.MyLoginModule"
module="deployment.${your deployment specific info goes here}">
<module-option name="my.package.MySecurityDataManager"
value="java:global/${your deployment specific info goes here}/MySecurityDataManager"/>
</login-module>
</authentication>
</security-domain>
Run Code Online (Sandbox Code Playgroud)
此时,您可以使用此安全域mydomain来管理容器中任何其他部署的安全性.
以下是一些使用场景:
mydomain安全域..war在其代码中附带预定义的安全注释.您的安全领域最初没有它们,因此没有用户可以登录.但是在部署之后,由于安全角色已有详细记录,您可以打开mydomain您编写的配置界面并声明这些角色,然后将用户分配给它们.现在他们可以登录了.mydomain,没有人能够使用它.最好的部分,特别是关于#2的部分是没有重新部署.此外,没有编辑XML来覆盖使用注释声明的默认安全设置(假设您的界面比这更好).
干杯! 我很乐意提供更多细节,但就目前而言,这至少应该告诉你是否需要它们.