如何在Spring Boot2中集成授权服务器和身份验证服务器(ldap)

时间:2019-12-28 17:06:51

标签: spring-boot authentication oauth-2.0 ldap authorization

我正在为我的Office项目构建一个授权服务器。他们使用LDAP作为用户身份验证,并且还包含用户权限和角色。

应该有用于用户登录的登录页面。一旦用户通过ldap身份验证获得身份验证并检查用户是否具有访问资源服务应用程序的正确角色,则授权服务器应向该用户颁发令牌。

我需要将LDAP身份验证集成为用户身份验证服务,并且它应该作为单独的服务运行。 我如何将其集成到我的授权服务器中。

我已经使用本教程中提到的嵌入式lDAP服务器创建了小型ldap身份验证服务

https://spring.io/guides/gs/authenticating-ldap/

另一件事是从外部客户端应用程序一侧,我应该将请求首先转发到哪个服务器以进行用户登录

我应该使用任何建筑模式吗?有人可以指出我正确的方向。

也可能会有很多资源服务

下面是我的授权服务器代码

@Configuration
@EnableAuthorizationServer
public class AuthorizeConfiguration extends AuthorizationServerConfigurerAdapter {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("saman").secret(bCryptPasswordEncoder.encode("saman"))
                .authorizedGrantTypes("password", "authorization_code", "refresh_token").scopes("read", "write")
                .accessTokenValiditySeconds(1200000000).authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT", "USER")
                .autoApprove(true);

    }

    @Override
    public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager)
                .accessTokenConverter(defaultAccessTokenConverter());
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(defaultAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter defaultAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

        logger.info(" hhhhhhhhhhhhhhhhhhhhhhhh "+bCryptPasswordEncoder.encode("saman"));
        // converter.setSigningKey("123");
        converter.setSigningKey("saman");
        // converter.setSigningKey(bCryptPasswordEncoder.encode("saman"));

        // converter.
        return converter;

    }
}

网络安全配置文件

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.authorizeRequests().antMatchers("/oauth/token").permitAll()
        .anyRequest().authenticated();
        http.formLogin().permitAll();

    }

      @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.inMemoryAuthentication()
          .withUser("saman").password( bCryptPasswordEncoder().encode("saman")).roles("USER");
    }

    @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
      }

      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
      }
}

0 个答案:

没有答案