Spring Security Rest API не защищен с помощью keycloak

Я интегрировал keycloak с устаревшим весенним приложением. Я добавил адаптер безопасности keycloak spring в свой файл pom.xml и добавил конфигурацию безопасности. После всего, что сделано, я могу получить доступ к остальным API без токена. Как мне решить эту проблему?

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
{
    /**
     * Registers the KeycloakAuthenticationProvider with the authentication manager.
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(keycloakAuthenticationProvider());
    }

    /**
     * Defines the session authentication strategy.
     */
    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        super.configure(http);
        http
                .authorizeRequests()
                .antMatchers("/school-admin*").hasRole("ADMIN")
                .anyRequest().permitAll();
    }
} 

keycloak.json

{
  "realm": "appscook",
  "bearer-only": true,
  "auth-server-url": "http://localhost:8080/auth",
  "ssl-required": "external",
  "resource": "ssd-backend"
}

API

  @RequestMapping(value="/hello",method=RequestMethod.GET)
    @ResponseBody
     public String getStandards(){
        return "hello";
    }

person boycod3    schedule 20.04.2017    source источник


Ответы (2)


Не могли бы вы попробовать, следуя функции настройки.

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

Также вы можете использовать аннотацию @PreAuthorize("hasRole('ADMIN')") для функции контроллера со следующей конфигурацией Spring;

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter
person Mustafa Onur AYDIN    schedule 17.10.2017

«/hello» не соответствует «/school-admin*», указанному в вашем методе конфигурации.

person Sébastien Blanc    schedule 20.04.2017
comment
/hello находится внутри сопоставления запросов на уровне класса /school-admin* - person boycod3; 22.04.2017
comment
А вы пробовали добавить косую черту перед * /school-admin/* - person Sébastien Blanc; 25.04.2017