05-oauth2-AuthorizationServer

2020-06-17  本文已影响0人  16325

AuthorizationServer

配置客户端详情(Client Details)

ClientDetailsServiceConfigurer 能够使用内存或 JDBC 方式实现获取已注册的客户端详情,有几个重要的属性:

管理令牌(Managing Token)

JWT 令牌(JWT Tokens)

配置授权类型(Grant Types)

配置授权端点 URL(Endpoint URLs)

AuthorizationServerEndpointsConfigurer 配置对象有一个 pathMapping() 方法用来配置端点的 URL,它有两个参数:

下面是一些默认的端点 URL:
/oauth/authorize:授权端点
/oauth/token:令牌端点
/oauth/confirm_access:用户确认授权提交端点
/oauth/error:授权服务错误信息端点
/oauth/check_token:用于资源服务访问的令牌解析端点
/oauth/token_key:提供公有密匙的端点,如果你使用JWT令牌的话
授权端点的 URL 应该被 Spring Security 保护起来只供授权用户访问

websecurity配置

使用认证服务器,必须同时配置websecurity
并且优先级一定要高于认证的配置

@Order(99)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
                .anyRequest().authenticated()
                .and().httpBasic()
                .and().csrf().disable();  //暂时禁用CSRF
    }
    /**
     * 授权验证服务
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
                .withUser("simm").password("123").roles("USER").and()
                .withUser("admin").password("admin").roles("USER","ADMIN");
    }
}

认证服务器的配置


@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {



    // 配置 password 授权模式
    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        clients.inMemory()
                .withClient("net5ijy")
                .secret("12345678")
                .authorizedGrantTypes("authorization_code") //授权模式为password和refresh_token两种
                .scopes("all")
                .redirectUris("http://localhost:8087/login");
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        // 表示支持 client_id 和 client_secret 做登录认证
        security.allowFormAuthenticationForClients();
    }
}

验证

现在的配置是授权码的方式,现在,通过访问如下url

http://localhost:7002/oauth/authorize?client_id=net5ijy&response_type=code&redirect_uri=http://localhost:8087/login

就会跳转的认证服务器,并且是httpbasic的认证。输入用户密码之后,跳回资源服务器,并携带code信息

上一篇 下一篇

猜你喜欢

热点阅读