首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Websphere基于角色的WS-Security with UsernameToken

Websphere基于角色的WS-Security with UsernameToken
EN

Stack Overflow用户
提问于 2015-12-09 23:46:36
回答 2查看 692关注 0票数 0

通过Websphere Console,我设置了一个策略集和一个策略集绑定,以便在Set服务上支持UsernameToken身份验证。正如预期的那样,它拒绝没有正确用户名和密码的web服务调用。但是,它现在接受连接的LDAP中的每个用户。

我希望能够仅允许特定LDAP组中的用户访问。我感觉我需要在Caller设置中创建一个自定义JAAS Login,但我不能完全确定。

有没有人对此有解决方案,或者我应该关注的方向?

编辑:我这样做是为了公开一个IBM BPM web服务。

EN

回答 2

Stack Overflow用户

发布于 2015-12-10 18:58:28

基于EJB而不是POJO创建您的web服务,然后使用@RolesAllowed注释指定允许从您的服务调用特定方法的角色。使用adminconsole、scirpt或binding文件将定义的角色映射到LDAP服务器中的用户或组。

这可能比使用Login模块战斗要容易得多,而且更灵活。

票数 0
EN

Stack Overflow用户

发布于 2016-11-04 03:05:44

您可以创建一个自定义JAAS登录模块,以便在使用用户名令牌时使用。您可以使用JAAS配置,该配置首先调用内置令牌消费者,然后调用您的自定义消费者。这样做意味着您可以使用内置的使用者来解析令牌并执行时间戳和随机数处理,并且只需在您自己的登录模块中进行用户名/密码验证。

可在此处找到相关说明:http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-nd-dist&topic=twbs_replace_authmethod_usernametoken

(请原谅我的格式。我正在尽我所能地利用我在这里提供的资源。)

使用堆叠的JAAS登录模块替换UsernameToken使用者的身份验证方法

默认情况下,Web服务安全UsernameToken使用者UNTConsumeLoginModule始终根据WebSphere注册表验证令牌中包含的用户名和密码。您可以使用GenericSecurityTokenFactory提供的SPI绕过此身份验证方法。

关于此任务

如果您想要替换UNTConsumeLoginModule使用的身份验证方法,则必须提供您自己的自定义JAAS登录模块来执行身份验证。定制登录模块堆叠在定制JAAS配置中的UNTConsumeLoginModule下。UNTConsumeLoginModule使用并验证令牌XML。为用户名和密码提供的值的验证将推迟到自定义堆叠登录模块。

由于UNTConsumeLoginModule的使用带有用户名和密码将经过身份验证的假设,因此对打算执行此功能的堆叠登录模块提出了比仅用于提供动态令牌功能的登录模块更多的要求。

要指示UNTConsumeLoginModule不应对用户名和密码进行身份验证,您必须在已配置的回调处理程序上设置以下属性:

代码语言:javascript
复制
com.ibm.wsspi.wssecurity.token.UsernameToken.authDeferred=true

与大多数WS-Security登录模型一样,UNTConsumeLoginModule始终将使用的令牌放在堆栈中的所有登录模块都可以访问的共享状态映射中。如果指定了authDeferred=true,则在提交阶段,UNTConsumeLoginModule将确保最初置于共享状态的相同UsernameToken对象已置于共享状态的另一个位置。如果找不到此UsernameToken对象,则会发生LoginException。因此,您不能只在回调处理程序上设置authDeferred=true,而不让附带的登录模块将令牌返回到共享状态。

操作步骤

JAAS

  • 开发一个登录模块来执行身份验证,并使其可用于您的应用程序代码。这个新的登录模块堆叠在com.ibm.ws.wssecurity.wssapi.token.impl.UNTConsumeLoginModule.下

此登录模块必须:

代码语言:javascript
复制
- Use the following method to get the UsernameToken that UNTConsumeLoginModule consumes.

用户名Token unt = UsernameToken)factory.getConsumerTokenFromSharedState(sharedState,UsernameToken.ValueType);

在此代码示例中,factory是com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory.的一个实例

您可以调用unt.getUsername()和unt.getPassword()来获取用户名和密码。

如果存在身份验证错误,您的登录模块应该抛出LoginException。

-将上一子步骤中获得的UsernameToken放回共享状态。

使用以下方法将UsernameToken重新置于共享状态。

factory.putAuthenticatedTokenToSharedState(sharedState,unt);

以下是一个登录模块示例:

代码语言:javascript
复制
package test.tokens;

import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory;
import com.ibm.websphere.wssecurity.wssapi.WSSUtilFactory;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken;

import java.util.ArrayList;

import com.ibm.wsspi.security.registry.RegistryHelper;
import com.ibm.websphere.security.UserRegistry;

public class MyUntAuthenticator implements LoginModule {

  private Map _sharedState;
  private Map _options;
  private CallbackHandler _handler;

  public void initialize(Subject subject, CallbackHandler callbackHandler,
                         Map<String, ?> sharedState, Map<String, ?> options) {

    this._handler = callbackHandler;
    this._sharedState = sharedState;
    this._options = options;  
  }

  public boolean login() throws LoginException {
    //For the sake of readability, this login module does not
    //protect against all NPE's

    GenericSecurityTokenFactory factory = null;
    WSSUtilFactory utilFactory = null;
    try {
      factory = GenericSecurityTokenFactory.getInstance();
      utilFactory = WSSUtilFactory.getInstance();
    } catch (Exception e) {
      throw new LoginException(e.toString());
    }
    if (factory == null) {
      throw new LoginException("GenericSecurityTokenFactory.getInstance() returned null");
    }

    UsernameToken unt = (UsernameToken)factory.getConsumerTokenFromSharedState(this._sharedState,UsernameToken.ValueType);

    String username = unt.getUsername();
    char [] password = unt.getPassword();

    //authenticate the username and password 
    //to validate a PasswordDigest password (fixpack 8.5.5.8 and later)
    //String pw = yourCodeToLookUpPasswordForUsername(username);
    //boolean match = utilFactory.verifyDigestedPassword(unt, pw.toCharArray());
    //if (!match) throw new LoginException("Digested passwords do not match");
    //Example:
    try {
      simpleUserGroupCheck(username, password, "cn=group1,o=ibm,c=us");
    } catch (Exception e) {
      LoginException le = new LoginException(e.getMessage());
      le.initCause(e);
      throw le;
    }

    //Put the authenticated token to the shared state
    factory.putAuthenticatedTokenToSharedState(this._sharedState, unt);

    return true;
  }

  private boolean simpleUserGroupCheck(String username, char [] password, String group) throws Exception {
    String allowedGroup = null;

    //get the default user registry
    UserRegistry user_reg = RegistryHelper.getUserRegistry(null);

    //authenticate the user against the user registry
    user_reg.checkPassword(username, new String(password));

    //get the list of groups that the user belongs to
    java.util.List<String> groupList = user_reg.getGroupsForUser(username);

    //you can either use a hard-coded group
    allowedGroup = group;

    //or get the value from your own custom property on the callback handler
    //WSSUtilFactory util = WSSUtilFactory.getInstance();
    //Map map = util.getCallbackHandlerProperties(this._handler);
    //allowedGroup = (String) map.get("MY_ALLOWED_GROUP_1");

    //check if the user belongs to an allowed group
    if (!groupList.contains(allowedGroup)) {
      throw new LoginException("user ["+username+"] is not in allowed group ["+allowedGroup+"]");
    }
    return true;
}
  //implement the rest of the methods required by the
  //LoginModule interface
}

JAAS

  • 创建新的JAAS登录configuration.

代码语言:javascript
复制
- In the administrative console, select Security > Global security.
- Under Authentication, select Java Authentication and Authorization Service.
- Select System logins.
- Click New, and then specify Alias = test.consume.unt.
- Click New, and then specify Module class name =     com.ibm.ws.wssecurity.wssapi.token.impl.UNTConsumeLoginModule
- Click OK.
- Click New, and then specify Module class name = test.tokens.MyUntAuthenticator
- Select Use login module proxy.
- Click OK, and then click SAVE. 

JAAS configuration.将您的令牌使用者配置为使用新的

代码语言:javascript
复制
- Open your bindings configuration that you want to change.
- In the administrative console, select WS-Security > Authentication and protection.
- Under Authentication tokens, select the UsernameToken inbound token that you want to change.
- Select JAAS login = test.consume.unt.

  1. 在为UsernameToken使用者配置的回调处理程序上设置required属性。

代码语言:javascript
复制
- Click Callback handler.
- Add the com.ibm.wsspi.wssecurity.token.UsernameToken.authDeferred=true custom property.
- Click OK.

单击SAVE.

  • Restart
  1. 以应用您的服务的JAAS配置。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34183038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档