当前位置:网站首页>Authorization server (simple construction of authorization server)
Authorization server (simple construction of authorization server)
2022-04-23 07:41:00 【cqwoniu】
1. stay pom Add dependency... To the file
<!-- Service discovery -->
<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. Add a profile
spring:
application:
name: authorization-server
cloud:
nacos:
discovery:
server-addr: nacos-server:8848
server:
port: 9999
3. Create startup class
@SpringBootApplication
public class AuthorizationApplication {
public static void main(String[] args) {
SpringApplication.run(AuthorizationApplication.class, args);
}
}
4. Add configuration class
4.1 Configuration of authorization server
package com.bjsxt.config;
@EnableAuthorizationServer// Enable the authorization service function
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
/** * Add a third-party client * */
@Override
// Input config Will pop up automatically configure Methods , Note that the parameter to be selected here is client Of
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("coin-api")// The name of the third party client
.secret(passwordEncoder.encode("coin-secret"))// The key of the third party client
.scopes("all")// Scope of authorization for third party clients
.accessTokenValiditySeconds(3600)//token The validity of the
.refreshTokenValiditySeconds(7*3600);//redresh_token The validity of the
super.configure(clients);
}
/** * Configure the Validation Manager ,UserdetailService */
@Override
// Also here configure Methods should also pay attention to the choice of endpoint
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
super.configure(endpoints);
}
}
4.2 web Secure configuration
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/** * Inject an authentication manager * * @return * @throws Exception */
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/** * Release of resources */
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); // close scrf
http.authorizeRequests().anyRequest().authenticated();
// Notice that... Is deleted here super class , Prevent conflict and error reporting when the service is started ,
}
/** * Create a test of UserDetail * @return */
@Bean
public UserDetailsService userDetailsService() {
//InMemoryUserDetailsManager yes Spring Security Config Provide a security configurator
// The security builder provides a user account details management object that stores user account details based on memory
// It is mainly used in the development and debugging environment , Its design purpose is mainly to test and function demonstration , Generally not used in production environment .
InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
// Note the imported user My bag is org.springframework.security.core.userdetails.User;
User user = new User("admin", "123456", Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"))) ;
inMemoryUserDetailsManager.createUser(user);
return inMemoryUserDetailsManager;
}
/** * The authentication manager that injects the password * @return */
@Bean
public PasswordEncoder passwordEncoder() {
// Note that this unencrypted method has been abandoned , We're just here to test
return NoOpPasswordEncoder.getInstance();
}
}
5. utilize postman Tools to test
5.1 postman Download and install
If this machine has been installed postman, Then this step ignores
Download address postman
Fool installation
5.2
版权声明
本文为[cqwoniu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230622032374.html
边栏推荐
- 反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
- Applet Wx Previewmedia related problem solving - Daily stepping on the pit
- 开发板如何ping通百度
- VR、AR、MR的区别与应用,以及对AR技术的一些实现原理
- ESP32学习-向工程项目添加文件夹
- SAP PI/PO rfc2Soap 发布rfc接口为ws示例
- 推导式与正则式
- Background management system framework, there is always what you want
- ‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
- Two threads print odd and even numbers interactively
猜你喜欢
Design optimization of MySQL database
简易随机点名抽奖(js下编写)
组合数求解与(扩展)卢卡斯定理
keytool: command not found
Visualization Road (IX) detailed explanation of arrow class
如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
学习笔记6-几种深度学习卷积神经网络的总结
Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet
Date对象(js内置对象)
菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
随机推荐
12.约束
Mysql隔离级别
14. Transaction processing
Discussion on arrow function of ES6
VScode
10.更新操作
The difference between null and undefined
typescript字典的使用
对STL容器的理解
Nacos / sentinel gateway current limiting and grouping (code)
推导式与正则式
manjaro安装与配置(vscode,微信,美化,输入法)
13. User and authority management
Pycharm
海康威视面经总结
h5本地存储数据sessionStorage、localStorage
[ACM-ICPC 2018 沈阳赛区网络预赛] J.Ka Chang (分块+dfs序)
[牛客挑战赛47]C.条件 (bitset加速floyd)
(扩展)BSGS与高次同余方程
Mysql 数据库从设计上的优化