Spring MVC+pring-security-oauth2 xml配置等注意事项 jdk1.7

maven引入jar包(适配jdk1.7老项目)

<dependency>
		  <groupId>org.springframework.security.oauth</groupId>
		  <artifactId>spring-security-oauth2</artifactId>
		  <version>2.0.7.RELEASE</version>
	  </dependency>
	  <dependency>
		  <groupId>org.springframework.security</groupId>
		  <artifactId>spring-security-jwt</artifactId>
		  <version>1.0.4.RELEASE</version>
	  </dependency>

需要在web.xml中增加拦截器

<filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

编写资源服务器

@Configuration
@EnableResourceServer //启用资源服务器
@EnableWebSecurity
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    /**
     * 声明了资源服务器的TokenStore是JWT
     * @param resources
     * @throws Exception
     */
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        //resourceId 还未设置
        resources.resourceId("serverId").tokenStore(tokenStore());
    }

    /**
     * 配置公钥
     * @return
     */
    @Bean
    protected JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource("public.cert");
        String publicKey = null;
        try {
            publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        converter.setVerifierKey(publicKey);
        return converter;
    }

    /**
     * 配置TokenStore
     *
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 配置了除了/user路径之外的请求可以匿名访问
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**").authenticated()
                .anyRequest().permitAll();
    }

}

applicationContext.xml配置扫描该包,注入bean 

<context:component-scan base-package="com.***.***"></context:component-scan>

至此,完成一个基于jwt验证的oauth2的资源服务器!

要说的坑就是!

@EnableWebSecurity,这个注解在非spring boot项目中,需要加上该注解,不然会报

No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found.......  异常

详细说明:https://blog.csdn.net/mingtiandexia/article/details/88910370

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值