我想要实现的目标:
现在,我有登录页面,可以验证用途(数据库检查)。 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时,我被重定向到登录页面,但登录后,我只是被重定向到登录页面。尽管在数据库中验证了正确的用户...有任何帮助吗?