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();
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

-七秒钟记忆

微薄打赏,小编的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值