将Spring Boot OAuth隐式授权与Angular应用集成

时间:2019-05-30 12:28:54

标签: angular spring-boot oauth-2.0

我用oAuth2隐式授予创建了Spring Boot应用程序。我可以从浏览器进行测试。我打算将其与angular 2应用程序集成。我被困住了,因为我无法使用用户ID和密码进行身份验证,因为我曾经使用过location.href和浏览器提示输入用户ID和密码。输入详细信息后,我将被重定向到具有访问令牌的指定对象。我正在寻找一种避免浏览器提示用户详细信息并显示登录页面以及能够从浏览器获取访问令牌的方法。

authGrantType(userName:string, password:string){
            //Not able to pass User id and password 
            let url = "http://localhost:8080/oauth/authorize?response_type=code&client_id=authClient1&redirect_uri=http://localhost:4200/users"
            location.href = url;
        }

@Configuration
@EnableWebSecurity
//@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private DataSource ds;

    @Autowired
    private ClientDetailsService clientDetailsService;


    @Bean("userDetailsService")
    protected UserDetailsService userDetailsService() {
        JdbcDaoImpl jd = new JdbcDaoImpl();
        jd.setDataSource(ds);
        return jd;

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
            .antMatchers("index.html")
            .antMatchers("/**/*.js")
            .antMatchers("/**/*.css")
            .antMatchers("/");
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .requestMatchers(CorsUtils:: isPreFlightRequest).permitAll()
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .csrf().disable()
        .anonymous().disable()
        .authorizeRequests()
        .antMatchers("/oauth/**").permitAll()
        .and()
        .httpBasic();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public JdbcTokenStore tokenStore() {
        return new JdbcTokenStore(ds);
    }

    @Bean
    @Autowired
    public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
        TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
        handler.setTokenStore(tokenStore);
        handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
        handler.setClientDetailsService(clientDetailsService);
        return handler;
    }

    @Bean
    @Autowired
    public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
        TokenApprovalStore store = new TokenApprovalStore();
        store.setTokenStore(tokenStore);
        return store;
    }

}

0 个答案:

没有答案