Spring Security OAuth2学习记录(二)

Spring Security OAuth2学习记录(二),第1张

Spring Security OAuth2学习记录(二)

继续上一次的文章 https://mp.csdn.net/mp_blog/creation/editor/108460839

密码模式流程:

1、用户(张三)向客户端(微博)提供账号密码(微信的账号密码)。

2、客户端将用户的账号密码发给认证服务器换取Token。

3、客户端携带Token去获取用户被保护的数据。

结合具体代码:

1、开启密码模式需要注入AuthenticationManager类,所以先要将其添加到容器中。

修改SpringSecurityConfig

package com.cmxy.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity //开启SpringSecurity 过滤链
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")//用户名
                .password(passwordEncoder.encode("123456"))//密码
                .authorities("admin");
    }

    
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

2、在认证配置类中,指定认证管理器

package com.cmxy.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.stereotype.Component;


@Component
@EnableAuthorizationServer //标记为认证服务器
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()//基于内存的模式
                .withClient("client")//客户端ID
                .secret(passwordEncoder.encode("123456"))//客户端秘钥,这里一定要加密。SpringSecurity 5.0之后规定密码必须加密。
                .resourceIds("product")//能访问的资源ID
                .scopes("user")//能访问的范围
                //授权方式:即当前认证服务器支持的授权方式分贝为 授权码模式、密码模式、客户端模式、简易模式、刷新Token
                .authorizedGrantTypes("authorization_code", "password", "client_credentials", "implicit", "refresh_token")
                .autoApprove(false)//是否自动授权,一般设置为false表示需要用户手动授权
                .redirectUris("https://www.baidu.com");//重定向路径
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //密码模式需要指定认证管理器
        endpoints.authenticationManager(authenticationManager);
    }
}

3、测试采用密码模式获取Token

简化模式:简化模式与授权码模式比较类似。不同的是简化模式不需要获取授权码,直接在浏览器url中返回令牌,该模式适用于不需要后端的应用(如Javascript应用)。

步骤:

直接在浏览器输入,跳转到登陆页,输入用的账号密码,页面跳转到之前配置的重定向路径,并且会附带Token

http://localhost:8080/oauth/authorize?client_id=client&response_type=token

 客户端模式:与之前的流程一样,只需要将grant_type 设置为客户端模式

 

 

 未完待续。。。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4670301.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-06
下一篇2022-11-06

发表评论

登录后才能评论

评论列表(0条)

    保存