无法通过spring安全认证
AuditEvent [timestamp = 2018-07-05T12:08:06.306Z, Principal = anonymousUser,类型= AUTHORIZATION_FAILURE, 数据= {详细信息= org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId: EF4FF849385896FA97E6E071393AC5E7, type = org.springframework.security.access.AccessDeniedException, message =访问被拒绝}]
我的配置:
@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{
//@Autowired
private AuthenticationManager authenticationManagerBean;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("clientId")
.secret("secret")
.authorizedGrantTypes("authorization_code")
.scopes("user_info")
.autoApprove(true);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean);
}
}
@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter{
//@Autowired
private AuthenticationManager authenticationManagerBean;
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers("/login", "/oauth/authorize")
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll()
.and()
.csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.parentAuthenticationManager(authenticationManagerBean)
.inMemoryAuthentication()
.withUser("sumit")
.password("sumit")
.roles("USER");
}
}
application.properties
server.port=8081
server.servlet.context-path=/auth
@RestController
@RequestMapping("/rest/hello")
public class HelloResource {
@GetMapping("/principal")
public Principal user(Principal principal) {
return principal;
}
@GetMapping
public String hello() {
return "hello world";
}
}
答案 0 :(得分:0)
更改此行:
.antMatchers("/login", "/oauth/authorize")
收件人:
.antMatchers("/login", "/oauth/authorize").permitAll()
您可能会在尝试授权时陷入困境。