【问题标题】:Migrating to keycloak for an app that uses spring security为使用 Spring Security 的应用迁移到 keycloak
【发布时间】:2016-01-05 13:03:57
【问题描述】:

我正在寻找为当前使用 Spring 安全性的 Spring MVC 应用程序设置密钥斗篷的步骤。

我想在Sitewhere 中使用keycloak。

【问题讨论】:

    标签: spring spring-mvc spring-security keycloak


    【解决方案1】:

    如果我能完整阅读 keycloak 的文档,我想这太简单了:)。无论如何,这是我在 Sitewhere 中迁移到 keycloak 时所遵循的步骤。

    1. 按照spring-security 的 keycloak 文档中给出的步骤进行操作
    2. 将依赖项添加到 sitewhere-core 和 sitewhere-web pom.xml,如 adapter installation 中所述
    3. 还在 sitewhere-web 的 pom.xml 中添加 jboss-logging 依赖项,因为 keycloak spring 适配器对 jboss-logging 具有硬编码依赖项。
    4. 修改 applicationcontext.xml 以便它可以在 web 和 api 中使用 keycloak,遵循 api 示例

      <sec:http pattern="/api/**"  entry-point-ref="keycloakAuthenticationEntryPoint">
      <sec:custom-filter ref="keycloakPreAuthActionsFilter" before="LOGOUT_FILTER" />
      <sec:custom-filter ref="keycloakAuthenticationProcessingFilter" before="FORM_LOGIN_FILTER" />
      

    5. 如下修改LoginManager.java

      public static IUser getCurrentlyLoggedInUser() throws SiteWhereException {
      Authentication KeyCloakAuth = SecurityContextHolder.getContext().getAuthentication();
      if (KeyCloakAuth == null) {
          throw new SiteWhereSystemException(ErrorCode.NotLoggedIn, ErrorLevel.ERROR,
                  HttpServletResponse.SC_FORBIDDEN);
      }
      
      KeycloakAccount  keyAccount = ((KeycloakAuthenticationToken) KeyCloakAuth).getAccount();
      
      String username = keyAccount.getKeycloakSecurityContext().getIdToken().getPreferredUsername();
      String password = "";
      
      IUser user = SiteWhere.getServer().getUserManagement().authenticate(username, password);
      List<IGrantedAuthority> auths =
              SiteWhere.getServer().getUserManagement().getGrantedAuthorities(user.getUsername());
      SitewhereUserDetails details = new SitewhereUserDetails(user, auths);
      
      
      Authentication auth = new SitewhereAuthentication(details, password);
      
      if (!(auth instanceof SitewhereAuthentication)) {
          throw new SiteWhereException("Authentication was not of expected type: "
                  + SitewhereAuthentication.class.getName() + " found " + auth.getClass().getName()
                  + " instead.");
      }
      return (IUser) ((SitewhereAuthentication) auth).getPrincipal();
      

      }

    6. 因为,我们已经将我们的身份验证迁移到 keycloak,并且我们不会在 siter 中获取用户的凭据,最好在 IUserManagement 的身份验证方法中取消与密码验证相关的代码。以下是来自 MongoUserManagement.java 的示例

      public IUser authenticate(String username, String password) throws SiteWhereException {
      if (password == null) {
          throw new SiteWhereSystemException(ErrorCode.InvalidPassword, ErrorLevel.ERROR,
                  HttpServletResponse.SC_BAD_REQUEST);
      }
      DBObject userObj = assertUser(username);
      String inPassword = SiteWherePersistence.encodePassoword(password);
      User match = MongoUser.fromDBObject(userObj);
      //nullify authentication since we are using keycloak
      /*if (!match.getHashedPassword().equals(inPassword)) {
          throw new SiteWhereSystemException(ErrorCode.InvalidPassword, ErrorLevel.ERROR,
                  HttpServletResponse.SC_UNAUTHORIZED);
      }*/
      
      // Update last login date.
      match.setLastLogin(new Date());
      DBObject updated = MongoUser.toDBObject(match);
      DBCollection users = getMongoClient().getUsersCollection();
      BasicDBObject query = new BasicDBObject(MongoUser.PROP_USERNAME, username);
      MongoPersistence.update(users, query, updated);
      
      return match;}
      
    7. 确保您在 keycloak 中的用户具有更特定于 sitewhere 的相应角色。

    8. 更改您的主页,使其重定向到 keycloak 以进行身份​​验证。以下是重定向示例:

          Tracer.start(TracerCategory.AdminUserInterface, "login", LOGGER);
      try {
          Map<String, Object> data = new HashMap<String, Object>();
          data.put("version", VersionHelper.getVersion());
          String keycloakConfig = environment.getProperty("AUTHSERVER_REDIRECTION_URL");          
          if (SiteWhere.getServer().getLifecycleStatus() == LifecycleStatus.Started) {
              return new ModelAndView("redirect:"+keycloakConfig);
          } else {
              ServerStartupException failure = SiteWhere.getServer().getServerStartupError();
              data.put("subsystem", failure.getDescription());
              data.put("component", failure.getComponent().getLifecycleError().getMessage());
              return new ModelAndView("noserver", data);
          }
      } finally {
          Tracer.stop(LOGGER);
      }
      

    【讨论】:

      猜你喜欢
      • 2017-05-30
      • 2020-05-12
      • 1970-01-01
      • 2017-08-07
      • 2021-09-23
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      相关资源
      最近更新 更多