未来就是现在的延续,过去就是完成的现在

Springboot2.0 + OAuth2.0之密码模式之认证资源分离

综述:Springboot2.0 + OAuth2.0之密码模式之单项目集成 中讲了密码模式,但是其认证和资源都在同一个项目,是不是有点混乱。于是乎,为啥不做认证和资源分离呢?Just do it...

一、认证服务器

当然认证服务器还是用之前搭建的那个,只是需要将资源配置注释,如下修改即可:

package com.liuzj.oauth2server.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

/**
 * 这个类表明了此应用是OAuth2 的资源服务器,此处主要指定受资源服务器保护的资源链接
 * 默认情况下spring security oauth2的http配置会被WebSecurityConfigurerAdapter的配置覆盖
 *
 * @author liuzj
 * @date 2019-01-15
 */
//@Configuration
//@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()//禁用了csrf(跨站请求伪造)功能
                .authorizeRequests()//限定签名成功的请求
                //必须认证过后才可以访问;注意:hasAnyRole 会默认加上ROLE_前缀,而hasAuthority不会加前缀
                .antMatchers("/decision/**","/govern/**").hasAnyRole("user")
                .antMatchers("/admin/**").hasRole("admin")
                .antMatchers("/test/**").authenticated()
                // 免验证请求
                .antMatchers("/oauth/**").permitAll();
    }

}

二、资源服务

(1)项目结构如下(Maven项目)

(2)配置资源相关配置

package com.liuzj.resource.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;

/**
 * 资源服务器的配置,这个类表明了此应用是OAuth2 的资源服务器(访问资源都需要认证),此处主要指定了受资源服务器保护的资源链接
 *
 * @author liuzj
 * @date 2019-01-15
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {


    @Primary
    @Bean
    public RemoteTokenServices tokenServices() {
        final RemoteTokenServices tokenService = new RemoteTokenServices();
        tokenService.setCheckTokenEndpointUrl("http://localhost:8001/oauth/check_token");
        tokenService.setClientId("client");
        tokenService.setClientSecret("123456");
        return tokenService;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()//禁用了csrf(跨站请求伪造)功能
                .authorizeRequests()//限定签名成功的请求
                //必须认证过后才可以访问;注意:hasAnyRole 会默认加上ROLE_前缀,而hasAuthority不会加前缀
                .antMatchers("/decision/**","/govern/**").hasAnyRole("user")
                .antMatchers("/admin/**").hasRole("admin")
                .antMatchers("/test/**").authenticated()
                // 免验证请求
                .antMatchers("/oauth/**").permitAll();
    }
}

这样资源配置就搞好了

(3)进行测试

同样,如果你直接访问则会提示你:此资源访问需要认证。所以得带上 token 进行访问:

同样获取 token 然后带上 token 即访问成功!

posted @ 2019-01-16 19:10  lzj123  阅读(1127)  评论(0编辑  收藏  举报