当前位置:网站首页>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
边栏推荐
- SDC intelligent communication patrol management system of Nanfang investment building
- [CF 1425D]Danger of Mad Snakes(组合计数+容斥)
- H5 case development
- 免费开源充电桩物联网云平台
- Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
- 记录一个查询兼容性的网站,String.replaceAll()兼容性报错
- The difference between null and undefined
- 浅谈BFC(块格式化上下文)
- 保洁阿姨都能看懂的中国剩余定理和扩展中国剩余定理
- 积性函数前缀和——杜教筛
猜你喜欢

# 可视化常见绘图(二)折线图

Typora语法详解(一)

可视化常见绘图(一)堆叠图

Discussion on frame construction and technology selection of short video platform

Intelligent communication solution of Hainan Phoenix Airport

简单易懂的子集dp

quill-editor图片缩放、在一个页面使用多个富文本框、quill-editor上传图片地址为服务器地址

Flexible blind patch of ad hoc network | Beifeng oil and gas field survey solution

Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference

javscript获取文件真实后缀名
随机推荐
Discussion on the outline of short video technology
Solution of emergency communication system for major security incidents
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
自定义classloader并实现热部署-使用loadClass
[COCI]Lampice (二分+树分治+字符串哈希)
Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet
免费开源智能充电桩物联网SAAS云平台
快速下载vscode的方法
[hdu6833]A Very Easy Math Problem(莫比乌斯反演)
Transformer的pytorch实现
学习笔记6-几种深度学习卷积神经网络的总结
P1390 公约数的和(莫比乌斯反演)
Emergency air space integrated communication system scheme of Guangxi Power Grid
LATEX使用
不需要破解markdown编辑工具Typora
SDC intelligent communication patrol management system of Nanfang investment building
UDP基础学习
MVCC(多版本并发控制)
pytorch:关于GradReverseLayer实现的一个坑