当前位置:网站首页>AuthorizationServer(授权服务器的简单搭建)
AuthorizationServer(授权服务器的简单搭建)
2022-04-23 06:23:00 【cqwoniu】
1.在pom文件里添加依赖
<!-- 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
2.增加一个配置文件
spring:
application:
name: authorization-server
cloud:
nacos:
discovery:
server-addr: nacos-server:8848
server:
port: 9999
3.创建启动类
@SpringBootApplication
public class AuthorizationApplication {
public static void main(String[] args) {
SpringApplication.run(AuthorizationApplication.class, args);
}
}
4.增加配置类
4.1 授权服务器的配置
package com.bjsxt.config;
@EnableAuthorizationServer//开启授权服务功能
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
/** * 添加第三方客户端 * */
@Override
//输入config会自动弹出来configure的方法,这里注意要选择的是参数是client的
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("coin-api")//第三方客户端的名称
.secret(passwordEncoder.encode("coin-secret"))//第三方客户端的密钥
.scopes("all")//第三方客户端的授权范围
.accessTokenValiditySeconds(3600)//token的有效期
.refreshTokenValiditySeconds(7*3600);//redresh_token的有效期
super.configure(clients);
}
/** * 配置验证管理器,UserdetailService */
@Override
//同样这里的configure方法也要注意选择的是endpoint
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
super.configure(endpoints);
}
}
4.2 web安全的配置
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/** * 注入一个验证管理器 * * @return * @throws Exception */
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/** * 资源的放行 */
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); // 关闭scrf
http.authorizeRequests().anyRequest().authenticated();
//注意这里删除了super类,防止服务启动的时候冲突报错,
}
/** * 创建一个测试的UserDetail * @return */
@Bean
public UserDetailsService userDetailsService() {
//InMemoryUserDetailsManager是Spring Security Config提供的一个安全配置器
//安全构建器提供的是一个基于内存存储用户账号详情的用户账号详情管理对象
//主要应用于开发调试环境,其设计目的主要是测试和功能演示,一般不在生产环境中使用。
InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
//注意这里导入的user的包是org.springframework.security.core.userdetails.User;
User user = new User("admin", "123456", Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"))) ;
inMemoryUserDetailsManager.createUser(user);
return inMemoryUserDetailsManager;
}
/** * 注入密码的验证管理器 * @return */
@Bean
public PasswordEncoder passwordEncoder() {
//注意这个不加密的方法已经废弃掉了,我们这里仅仅用来测试
return NoOpPasswordEncoder.getInstance();
}
}
5.利用postman工具进行测试
5.1 postman下载安装
若本机已经安装postman,则此步骤忽略
下载地址postman
傻瓜式安装
5.2
版权声明
本文为[cqwoniu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cqwoniu/article/details/120544296
边栏推荐
猜你喜欢
Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
Meishe helps Baidu "Duka editing" to make knowledge creation easier
USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
Discussion on frame construction and technology selection of short video platform
可视化之路(九)Arrow类详解
不需要破解markdown编辑工具Typora
PC端一次启动多个微信
Discussion on the outline of short video technology
The people of Beifeng have been taking action
数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析
随机推荐
小程序换行符\n失效问题解决-日常踩坑
Jupyter Notebook 安装
如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
什么是闭包?
快速傅里叶变换FFT简明教程
自定义钉钉机器人进行报警
(扩展)BSGS与高次同余方程
Failed to install Tui editor, quick solution
两个线程交互打印奇偶数字
关于素数的不到100个秘密
华为云MVP邮件
C语言的指针符号到底靠近变量类型还是变量名?
VScode
Emergency air space integrated communication system scheme of Guangxi Power Grid
HQL语句的调优
获取字符格式的当前时间
快速下载vscode的方法
kaggle-房价预测实战
Applet Wx Previewmedia related problem solving - Daily stepping on the pit
1D/1D动态规划学习总结