- 29
- 0
在本来spring security的基础上使用了spring security oauth2,控制/api下的请求。浏览了很多网上的配置,但是测试时发现spring security的资源控制和spring securtiy oauth2的资源控制会互相覆盖,没法做到分离控制。如果配置添加了security.oauth2.resource.filter-order=3,则使用spring security的控制,反之则为oauth2的控制。
代码中我的配置如下:
Spring security配置:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserManagerService userManagerService;
@Override
@Bean //分享到oauth2
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 密码加密
*/
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 关闭csrf保护功能(跨域访问)
.csrf().disable()
.authorizeRequests()
.antMatchers("/oauth/**").permitAll()
.antMatchers("/**/*.js", "/**/*.css", "/**/*.png",
"/**/*.gif", "/**/*.jpg", "/**/*.jpeg", "/**/*.map",
"/**/*.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/user/login_page")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(new CustomSimpleUrlAuthenticationSuccessHandler())
.failureHandler(new CustomSimpleUrlAuthenticationFailureHandler())
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/user/login_page")
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userManagerService)
.passwordEncoder(passwordEncoder());
}
}
Spring security oatuth2配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration
extends AuthorizationServerConfigurerAdapter {
@Autowired
AuthenticationManager authenticationManager;
@Autowired
private UserManagerService userManagerService;
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.tokenStore(tokenStore())
.userDetailsService(userManagerService)
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security)
throws Exception {
// 允许表单认证
security
.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()
.withClient("cmdb")
.authorizedGrantTypes("password", "refresh_token")
.secret("api")
.scopes("xxx");
}
}
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration
extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").authenticated();
}
}
之前查阅过很多博客,也查过spring oauth2的几种模式的授权流程,但是都没有找到原因
- 共 0 条
- 全部回答
-
回憶↘無止境的痛 普通会员 1楼
Spring Security和OAuth2在许多方面都有相似之处,但它们在实现方式上有所不同。
首先,Spring Security是基于Spring框架的,它使用Spring Security核心,提供了许多安全功能,如身份验证、授权、日志管理等。而OAuth2是独立于Spring框架的,它是一种授权框架,允许客户端和服务器之间的身份验证和授权。
其次,Spring Security和OAuth2在实现方式上有所不同。Spring Security主要通过用户认证和授权功能实现资源控制,而OAuth2则主要通过访问令牌(Access Token)实现资源控制。
此外,Spring Security和OAuth2在使用场景上也有所不同。Spring Security主要用于web应用,而OAuth2则主要用于移动应用和企业应用。
因此,虽然Spring Security和OAuth2在实现方式上有所不同,但它们在许多方面都有相似之处,如实现资源控制。如果你需要同时使用Spring Security和OAuth2,建议你按照OAuth2的使用场景进行配置。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
