当前位置:网站首页>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
边栏推荐
猜你喜欢
Date对象(js内置对象)
Us photo cloud editing helps BiliBili upgrade its experience
直观理解熵
Background management system framework, there is always what you want
Design optimization of MySQL database
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
Machine vision series (01) -- Overview
Meishe helps Baidu "Duka editing" to make knowledge creation easier
快速下载vscode的方法
Mysql 索引
随机推荐
Visualization Road (IX) detailed explanation of arrow class
反思|开启B站少女心模式,探究APP换肤机制的设计与实现
Javscript gets the real suffix of the file
安装配置淘宝镜像npm(cnpm)
12.约束
经典套路:一类字符串计数的DP问题
推导式与正则式
[LNOI2014]LCA——树链剖分——多点LCA深度和问题
Authorization+Token+JWT
如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
学习笔记6-几种深度学习卷积神经网络的总结
[牛客练习赛68]牛牛的粉丝(矩阵快速幂之循环矩阵优化)
keytool: command not found
开发板如何ping通百度
8. Paging query
Mysql的存储引擎
LATEX使用
通用型冒泡、选择、插入、希尔、快速排序的代码实现
xdotool按键精灵
页面实时显示当前时间