栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Security OAuth2学习记录(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 设置为客户端模式

 

 

 未完待续。。。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340115.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号