实施帐户服务,休息模板以使用异步任务

时间:2018-04-28 07:54:37

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

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

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

这里是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?

1 个答案:

答案 0 :(得分:0)

您还需要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

更改它

enter image description here