带有Spring Security基本身份验证的Oauth2

时间:2016-12-29 06:58:52

标签: spring authentication login spring-security oauth

我有oauth2服务,我有一个客户端。我正在尝试使用我的oauth2服务登录,但我不断收到“用户必须登录才能获得身份验证”。我正在尝试将用户发送到oauth服务进行登录然后我正在尝试获取令牌,但是当我尝试弹出安全性登录打开时,我无法从我的oauth2服务获得身份验证。当我登录弹出安全登录时,我可以获得身份验证。我错过了什么吗?我需要帮助。

我的身份验证服务类:

@SpringBootApplication
@EnableResourceServer
@Order(6)
public class AuthServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthServiceApplication.class, args);
    }
}

@RestController
class PrincipalRestController {

    @RequestMapping({"/user", "/me"})
    Principal principal(Principal principal) {
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return principal;
    }

    @RequestMapping("/giris")
    Principal giris(Principal principal) {
        return principal;
    }
}

@Configuration
@EnableAuthorizationServer
class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public OAuthConfiguration(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

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

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(this.authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("acme")
                .secret("acmesecret")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .scopes("openid", "read", "write").autoApprove(".*");
    }
}


//@Component
//class AccountCLR implements CommandLineRunner {
//
//    @Override
//    public void run(String... strings) throws Exception {
//        Stream.of("jlong,spring", "pwebb,boot", "zeynep,Bisoft123").map(x -> x.split(",")).forEach(tuple -> this.accountRepository.save(new Account(tuple[0], tuple[1], true)));
//    }
//
//    private final AccountRepository accountRepository;
//
//    @Autowired
//    public AccountCLR(AccountRepository accountRepository) {
//        this.accountRepository = accountRepository;
//    }
//
//}
@Service
class AccountUserDetailService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return accountRepository.findByKullaniciAdi(username);
    }

    private final KullaniciRepository accountRepository;

    @Autowired
    public AccountUserDetailService(KullaniciRepository accountRepository) {
        this.accountRepository = accountRepository;
    }

}

//My web security config
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
//@EnableOAuth2Client
//@EnableAuthorizationServer
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.authorizeRequests().antMatchers("/css/**", "/metronic/css/**").permitAll()
                .and().authorizeRequests().antMatchers("/metronic/image/**", "/image/**", "/metronic/css/fonts/**", "/metronic/fonts/**").permitAll()
                .and().authorizeRequests().antMatchers("/js/**", "/metronic/js/**").permitAll()
                .and().httpBasic().and().authorizeRequests()
                .antMatchers("/login.html", "/language/**","/uaa/*", "/api/kullanici/user", "/logout", "/kilitEkrani.html", "/404.html").permitAll()
                .anyRequest().authenticated().and()
                .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class).csrf().csrfTokenRepository(csrfTokenRepository()).and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login.html")
                .permitAll().and().csrf().disable();
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

}

//my resource server
@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/me")
                .authorizeRequests().anyRequest().authenticated();//.and().csrf().csrfTokenRepository(csrfTokenRepository());
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

//    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth
//                .inMemoryAuthentication()
//                .withUser("zeynep").password("Bisoft123").roles("USER");
//    }
}

0 个答案:

没有答案