Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oauth2与Spring安全基本身份验证_Spring_Authentication_Login_Spring Security_Oauth - Fatal编程技术网

Oauth2与Spring安全基本身份验证

Oauth2与Spring安全基本身份验证,spring,authentication,login,spring-security,oauth,Spring,Authentication,Login,Spring Security,Oauth,我有一个oauth2服务和一个客户端。我正在尝试使用oauth2服务登录,但我一直得到“用户必须登录才能获得身份验证”。我尝试将用户发送到oauth服务以登录,然后尝试获取令牌,但当我尝试时,弹出的安全登录打开,我无法从oauth2服务获得身份验证。当我登录到弹出的安全登录,然后我可以得到认证。我错过什么了吗?求你了,我需要帮助 我的身份验证服务类: @SpringBootApplication @EnableResourceServer @Order(6) public class AuthS

我有一个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");
//    }
}

您可以共享您的代码。您可以检查此中的代码我在我的身份验证服务中添加了所有配置类扫描您可以共享您的代码。您可以检查此中的代码我在我的身份验证服务中添加了所有配置类