oauth2认证对某个接口进行放行

重点是区分自己写的配置类,所继承的有两个
WebSecurityConfigurerAdapter、ResourceServerConfigurerAdapter

先来看一下两个配置特别类似


/**
 * Web安全配置类
 * springSecurity安全管理框架配置类继承WebSecurityConfigurerAdapter
 * @版权所有 
 *
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private AuthServiceImpl authServiceImpl;


	/**
	 * 为特定的Http请求配置基于Web的安全约束
	 */
	@Override
	protected void configure(HttpSecurity httpSecurity) throws Exception {
		httpSecurity.authorizeRequests().anyRequest().authenticated().and().csrf().disable();
	}

	/**
	 * 配置认证信息
	 */
	@Override
	protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {

		authenticationManagerBuilder.authenticationProvider(authProvider()).userDetailsService(authServiceImpl).passwordEncoder(new BCryptPasswordEncoder());

	}

	/**
	 * 实例化AuthenticationManager对象,以处理认证请求
	 */
	@Override
	@Bean
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}


	@Bean
    public MyAuthProvider authProvider(){
		return new MyAuthProvider();
	}


}

我在配置对某个接口无需授权就可访问的时候,就会在这个配置类中直接进行配置,但一直报错,无法访问,后来看到了另一个类,配置好之后,一切正常



/**
 * @author :chaogry
 * @date :Created in 2020/8/10 9:29
 * @description:资源认证服务器,配置对/encrypt/publickey接口的放行
 * @modified By:
 * @version: $
 */
@Configuration
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    /**
     * @description:资源认证服务器,配置对/encrypt/publickey接口的放行
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .headers().frameOptions().disable()
                .and()
                .authorizeRequests().antMatchers("/encrypt/publickey").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }
}

小白一枚,没用过这种认证方式,所以记录一下

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于Spring Security OAuth2的认证授权代码示例: 1. 添加Maven依赖 ```xml <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.4.0.RELEASE</version> </dependency> ``` 2. 配置OAuth2服务器 ```java @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 3. 配置资源服务器 ```java @Configuration @EnableResourceServer public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated(); } } ``` 4. 配置Spring Security ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/oauth/**").permitAll().anyRequest().authenticated().and().csrf().disable(); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 5. 配置数据源 ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } } ``` 6. 配置应用属性 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/oauth2 spring.datasource.username=root spring.datasource.password=root security.oauth2.client.client-id=client security.oauth2.client.client-secret=secret security.oauth2.client.access-token-validity-seconds=3600 security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials security.oauth2.client.scope=read,write security.oauth2.resource.id=resource security.user.name=user security.user.password=password security.user.roles=USER ``` 以上代码仅供参考,具体实现需要根据实际场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-七秒钟记忆

微薄打赏,小编的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值