【问题标题】:Implement account service, rest template to use an async task实现帐户服务,休息模板以使用异步任务
【发布时间】:2018-10-08 23:56:31
【问题描述】:

我正在尝试使用 Keycloak 和 Spring Boot 实现服务帐户,以保护异步计划任务。

我认为我需要它,否则我没有访问我的服务的凭据。

这里是keycloak指南:https://www.keycloak.org/docs/latest/server_admin/index.html#_service_accounts

我尝试了类似的方法:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
    map.add("grant_type", "client_credentials");

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);


    ResponseEntity<String> response = keycloakRestTemplate.postForEntity(URI.create("http://MYURL/auth/realms/MYREALM/protocol/openid-connect/token"), request , String.class );

但我收到此错误:

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:85) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:636) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:431) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]

这是我的安全配置:

    Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
@KeycloakConfiguration
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {  

    @Value("${app.mux}")
    private String mux; 

    @Autowired
    public KeycloakClientRequestFactory keycloakClientRequestFactory;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
            ..................
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(keycloakAuthenticationProvider());
    }

    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public KeycloakRestTemplate keycloakRestTemplate() {
        return new KeycloakRestTemplate(keycloakClientRequestFactory);
    }

    @Bean
    public KeycloakConfigResolver keycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Bean
    public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean(KeycloakAuthenticationProcessingFilter filter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(KeycloakPreAuthActionsFilter filter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
           .ignoring()
           .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/webjars/**");
    }

So how can I implement a request like above?

【问题讨论】:

    标签: spring spring-boot scheduled-tasks resttemplate keycloak


    【解决方案1】:

    您还需要client_idclient_secret 将身份验证作为服务(我猜您的客户端类型是机密的)。来自Oauth2 spec

    2.3.1。客户密码

    拥有客户端密码的客户端可以使用 HTTP Basic
    [RFC2617] 中定义的身份验证方案,用于进行身份验证
    授权服务器。客户端标识符使用 附录中的“application/x-www-form-urlencoded”编码算法 B、编码后的值作为用户名;客户端 密码使用相同的算法进行编码并用作
    密码。授权服务器必须支持 HTTP Basic
    用于验证已发布的客户端的身份验证方案
    客户密码。

    例如(带有额外的换行符仅用于显示目的):

    授权:基本 czZCaGRSa3F0Mzo3RmpmcDBaQnIxS3REUmJuZlZkbUl3

    或者,授权服务器可以支持包括
    请求正文中的客户端凭据使用以下
    参数:

    client_id 必需的。期间发给客户端的客户端标识符 第 2.2 节描述的注册过程。

    client_secret 必需的。客户机密。客户端可以省略 如果客户端密码为空字符串,则为参数。

    所以将它们添加到您的代码中:

    map.add("client_id", "my_application");
    map.add("client_secret", "" /*Provide the value given by keycloak*/);
    

    您可以检索自动生成的客户端密码,甚至可以从 admin panel 更改它:

    【讨论】:

    • 谢谢!但我正在寻找使用 JWT 的解决方案...实际上我还阅读了以下问题,您已经对此发表了评论:stackoverflow.com/questions/49900124/… 但我不太清楚如何实现代码
    • 请在问题中添加您的全部要求。否则,您会让人们白白工作。
    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2010-12-25
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多