Java知识储备SpringFramework程序猿阵线联盟-汇总各类技术干货

Spring Boot整合Spring Security简记-O

2018-03-16  本文已影响684人  78240024406c

new無语 转载请注明原创出处,谢谢!

Spring Security学习目录

上接Spring Boot整合Spring Security简记-OAuth2(十一)

过滤器


相关类


请求端点


InMemory 基于内存存储令牌


添加客户端信息

@Configuration
@EnableAuthorizationServer
public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //添加客户端信息
        // 使用in-memory存储客户端信息
        clients.inMemory()
                // client_id
                .withClient("client")
                // client_secret
                .secret("secret")
                // 该client允许的授权类型
                .authorizedGrantTypes("authorization_code")
                // 允许的授权范围
                .scopes("app");
    }
}

设置basic登陆

@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic();
    }
}

之后请求端点地址,就可以访问授权。
GET response_type设置为code,获取code后,请求/oauth/token获取access_token。(authorized_grant_types设置为authorization_code

http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.jianshu.com

GET response_type设置为token直接获取access_token。(authorized_grant_types设置为implicit

http://localhost:8080/oauth/authorize?client_id=client&response_type=token&redirect_uri=http://www.jianshu.com

之后会请求登陆,默认登录名user,密码在控制台输出了。复制出来就OK了。

获取密码
登陆
跳转到很low的授权页面,点击按钮,就会授权获取code跳转到简书页面。
默认授权页面 授权成功回调页面

POST通过code获取access_token

http://client:secret@localhost:8080/oauth/token

参数

Content-Type:application/x-www-form-urlencoded

code:psvucZ
grant_type:authorization_code
redirect_uri:http://www.jianshu.com

POM依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>

JDBC 基于JDBC存储令牌


设置客户端信息存储方式为jdbc:

    @Autowired
    private DataSource dataSource;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
        .allowFormAuthenticationForClients()
        .passwordEncoder(new BCryptPasswordEncoder())
        .tokenKeyAccess("permitAll()")
        .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //添加客户端信息
        clients.jdbc(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authorizationCodeServices(new JdbcAuthorizationCodeServices(dataSource))
                .authenticationManager(authenticationManager);
    }
       if (authenticationManager != null) {
            tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
                    clientDetails, requestFactory));
        }

POST http://localhost:8080/oauth/token 通过账户/密码获取access_token
参数

Content-Type:application/x-www-form-urlencoded

password:password
username:user
client_id:client
client_secret:secret
grant_type:password
密码授权认证
上一篇 下一篇

猜你喜欢

热点阅读