誓言2 +春天+ jwt

时间:2016-03-10 18:43:47

标签: spring login oauth authorization jwt

我想要实现的目标:

  • 使用java配置的Spring中的Oauth2服务器
  • Oauth2服务器上的登录页面
  • 访问客户端的用户将被重定向到Oauth2服务器上的登录页面
  • 登录后,他会自动重定向到/ oauth / authorize以获取身份验证码(JWT-token)
  • 之后他被重定向回客户
  • 客户端将从oauth / token(JWT-token)
  • 获取访问令牌

现在,我有登录页面,可以验证用途(数据库检查)。 Oauth已配置,但每个路径都重定向到此登录页面(即使他已登录)

我的WebSecurityConfig:

@Autowired
MyUserDetailsService userService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        //.antMatcher("/**")
        .formLogin()
            .loginPage("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .permitAll()
        .and()
            .authorizeRequests()
            .antMatchers("oauth/authorize").access("hasRole('ROLE_ADMIN')")
            .antMatchers("oauth/token").access("hasRole('ROLE_ADMIN')")
        .and()
            .csrf()
        .and()
            .exceptionHandling().accessDeniedPage("/Access_Denied");




    // This works for basic authentication: 
    /* http.authorizeRequests()
        .antMatchers("/", "/home").permitAll()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .antMatchers("/db/**").access("hasRole('ROLE_ADMIN') and hasRole('DBA')")
        .and().formLogin().loginPage("/login")
        .usernameParameter("username").passwordParameter("password")
        .and().csrf()
        .and().exceptionHandling().accessDeniedPage("/Access_Denied");*/
}

我的AuthorizationServerConfiguration:

@Autowired
private AuthenticationConfiguration authenticationConfiguration;

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    return new JwtAccessTokenConverter();
}

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.tokenKeyAccess("isAnonymous() || hasRole('ROLE_ADMIN')")
            .checkTokenAccess("hasRole('ROLE_ADMIN')");
}

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


@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("some-client")
            .secret("some-client")
            .authorizedGrantTypes("authorization_code", "password", "implicit") //enabled all for testing
            .scopes("read", "trust")
            .redirectUris("http://anywhere?key=value");
}


@Autowired
public void setAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration) {
    this.authenticationConfiguration = authenticationConfiguration;
}

当我尝试访问/ oauth / authorize时,我被重定向到登录页面,但登录后,我只是被重定向到登录页面。尽管在数据库中验证了正确的用户...有任何帮助吗?

0 个答案:

没有答案