Как использовать базовую аутентификацию - PullRequest
0 голосов
/ 08 мая 2019

Я хочу добавить базовую аутентификацию к моим URI-ссылкам на пользовательские интерфейсы swagger на основе ролей. Я использую springfox 2.9.2, но не могу ограничить пользователей на основе ролей для доступа к любому API.

SecurityConfig.Java

@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManager();
}

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

    @Autowired
    public PasswordEncoder passwordEncoder;

    @Override
    public void configure(HttpSecurity http) throws Exception {

         http
         /*.sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()*/
        .authorizeRequests()
        .antMatchers("/swagger-resources/**", "*.html", "/api/v1/swagger.json")
            .hasRole("ADMIN")
        .anyRequest()
            .authenticated()
        .and().httpBasic()
        .and().anonymous();

    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


        String passKey1 = passwordEncoder.encode("12345");
        String passKey2 = passwordEncoder.encode("6789");
        auth
            .inMemoryAuthentication()
                .withUser("pqr").password(passKey1).roles("ADMIN").and()
                .withUser("abc").password(passKey2).roles("USER");


    }

}

...