html页面授权码,spring boot 2.0 整合 oauth2 authorization code授权码模式

oauth2 authorization code 大致流程

用户打开客户端后,客户端要求用户给予授权。

用户同意给予客户端授权。

客户端使用授权得到的code,向认证服务器申请token令牌。

认证服务器对客户端进行认证以后,确认无误,同意发放令牌。

客户端请求资源时携带token令牌,向资源服务器申请获取资源。

资源服务器确认令牌无误,同意向客户端开放资源。

security oauth2 整合的核心配置类

授权认证服务配置 AuthorizationServerConfiguration

security 配置 SecurityConfiguration

工程结构目录

5e3f732b81f4

image.png

pom.xml

security_oauth2_authorization

0.0.1-SNAPSHOT

jar

security_oauth2_authorization

oauth2 authorization_code 授权模式

org.springframework.boot

spring-boot-starter-parent

2.0.2.RELEASE

UTF-8

UTF-8

1.8

1.0.9.RELEASE

0.9.0

Finchley.RC2

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-oauth2

org.springframework.cloud

spring-cloud-starter-security

org.springframework.security

spring-security-jwt

${security-jwt.version}

org.springframework.boot

spring-boot-starter-freemarker

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

授权认证服务配置类 AuthorizationServerConfiguration

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired

private AuthenticationManager authenticationManager;

@Autowired

private MyUserDetailsService userDetailsService;

@Override

public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {

oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");

}

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

String secret = passwordEncoder.encode("secret");

clients.inMemory() // 使用in-memory存储

.withClient("client") // client_id

.secret(secret) // client_secret

//.autoApprove(true)   //如果为true 则不会跳转到授权页面,而是直接同意授权返回code

.authorizedGrantTypes("authorization_code","refresh_token") // 该client允许的授权类型

.scopes("app"); // 允许的授权范围

}

@Override

public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService)

.accessTokenConverter(accessTokenConverter())

.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST); //支持GET POST 请求获取token;

}

@Bean

public JwtAccessTokenConverter accessTokenConverter() {

JwtAccessTokenConverter converter = new JwtAccessTokenConverter() {

@Override

public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

String userName = authentication.getUserAuthentication().getName();

final Map additionalInformation = new HashMap();

additionalInformation.put("user_name", userName);

((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);

OAuth2AccessToken token = super.enhance(accessToken, authentication);

return token;

}

};

//converter.setSigningKey("bcrypt");

KeyPair keyPair = new KeyStoreKeyFactory(new ClassPathResource("kevin_key.jks"), "123456".toCharArray())

.getKeyPair("kevin_key");

converter.setKeyPair(keyPair);

return converter;

}

}

web 安全配置 WebSecurityConfig

@Order(10)

@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true)

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

private MyUserDetailsService userDetailsFitService;

@Override

@Bean

public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable()

.authorizeRequests()

.antMatchers("/","/oauth/**","/login","/health", "/css/**").permitAll()

.anyRequest().authenticated()

.and()

.formLogin()

.loginPage("/login")

.permitAll();

}

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsFitService).passwordEncoder(passwordEncoder());

auth.parentAuthenticationManager(authenticationManagerBean());

}

@Bean

public PasswordEncoder passwordEncoder(){

return PasswordEncoderFactories.createDelegatingPasswordEncoder();

}

}

url 注册配置 MvcConfig

@Configuration

public class MvcConfig implements WebMvcConfigurer {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/login").setViewName("login"); //自定义的登陆页面

registry.addViewController("/oauth/confirm_access").setViewName("oauth_approval"); //自定义的授权页面

}

}

# security 登陆认证 MyUserDetailsService

@Service

public class MyUserDetailsService implements UserDetailsService {

@Override

public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

if ("admin".equalsIgnoreCase(name)) {

User user = mockUser();

return user;

}

return null;

}

private User mockUser() {

Collection authorities = new HashSet<>();

authorities.add(new SimpleGrantedAuthority("admin"));

PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

String pwd = passwordEncoder.encode("123456");

User user = new User("admin",pwd,authorities);

return user;

}

}

自定义登陆页面 login.html

登陆

username

Password

登陆

自定义授权页面 oauth_approval.html

授权

你是否授权client_id=client访问你的受保护资源?

action="../oauth/authorize" method="post">

Approve

action="../oauth/authorize" method="post">

Deny

application.yml

server:

port: 18084

spring:

application:

name: oauth2-server # 应用名称

thymeleaf:

prefix: classpath:/templates/

logging:

level:

org.springframework.security: DEBUG

1. 访问oauth2 服务

client_id:第三方应用在授权服务器注册的 Id

response_type:固定值 code。

redirect_uri:授权服务器授权重定向哪儿的 URL。

scope:权限

state:随机字符串,可以省略

如果未登陆则出现登录页面,输入用户名:admin 密码:123456 登陆系统

5e3f732b81f4

image.png

2. 成功登陆后自动跳转到授权页面

5e3f732b81f4

image.png

3. 携带授权之后返回的code 获取token

5e3f732b81f4

image.png

这里的账号和密码 是我们注册的 client_id 和 client_secret

5e3f732b81f4

image.png

成功登陆后获取token

5e3f732b81f4

image.png

4.携带toekn 访问资源

demo地址:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540813.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Net设计模式实例之代理模式(Proxy Pattern)

一、代理模式简介&#xff08;Brief Introduction&#xff09; 代理模式&#xff08;Proxy Pattern&#xff09;对其他对象提供一种代理以控制对这个对象的访问。 二、解决的问题&#xff08;What To Solve&#xff09; 1、远程代理 远程代理&#xff0c;也就是为了一个对象…

c语言存储类_C编程语言的存储类

c语言存储类A variables storage class tells us the following, 变量的存储类告诉我们以下内容&#xff1a; Where the variables would be stored? 变量将存储在哪里&#xff1f; What will be the initial of the variable, if the initial value is not specifically ass…

jsonp请求html页面,JavaScript中的JSON和JSONP

简单地使用json并不能支持跨域资源请求&#xff0c;为了解决这个问题&#xff0c;需要采用jsonp数据交互协议。众所周知&#xff0c;js文件的调用不受跨域与否的限制&#xff0c;因此如果想通过纯web端跨域访问数据&#xff0c;只能在远程服务器上设法将json数据封装进js格式的…

2017软件工程实践

课程信息 软件工程实践 参考教材 《构建之法》 作者&#xff1a;邹欣&#xff0c; 编辑&#xff1a;周筠 他山之石 北京航空航天大学 罗杰&#xff0c; 刘乾 东北师范大学 杨贵福 北京电子科技学院 娄嘉鹏 教师&#xff1a;汪璟玢 助教&#xff1a;卞…

suse leap_Ruby程序检查leap年

suse leapProblem statement: Given a year, we have to check whether it is a Leap year or not using Ruby program. 问题陈述 &#xff1a;给定年份&#xff0c;我们必须使用Ruby程序检查是否为Le年。 Methods used: 使用的方法&#xff1a; gets(): This method is a pu…

html导航栏点击不能跳转,无法单击导航栏中的链接CSS HTML

不确定是否允许您链接您的网站&#xff0c;但是如果允许。 因此&#xff0c;我可以将所有链接悬停在导航栏中&#xff0c;但我无法点击它们&#xff0c;并且S的图片是可移动的&#xff0c;但无法点击&#xff0c;我做错了什么&#xff1f;无法单击导航栏中的链接CSS HTMLNickeb…

JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址

2019独角兽企业重金招聘Python工程师标准>>> 在写Java程序时不可避免要获取文件的路径...总结一下,遗漏的随时补上 1.可以在servlet的init方法里 String path getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 :E:\eclipseM9\worksp…

关于细分到字段的权限系统_操作系统中的细分

关于细分到字段的权限系统为什么需要细分&#xff1f; (Why Segmentation is required?) In the Operating System, an important drawback of memory management is the separation of the users view of memory and the actual physical memory. Paging is the scheme which…

计算机科学技术专业解析,专业解读—计算机科学与技术

原标题&#xff1a;专业解读—计算机科学与技术专业培养目标&#xff1a;本专业培养具有良好的科学素养&#xff0c;系统地、较好地掌握计算机科学与技术包括计算机硬件、软件与应用的基本理论、基本知识和基本技能与方法&#xff0c;能在科研部门、教育单位、企业、事业、技术…

阿里云服务器配置开发环境第五章:Centos7.3切换为iptables防火墙

centos7.3默认使用的防火墙应该是firewall&#xff0c;而不是iptables。而我们xxmj服务器使用的是iptables防火墙。所以&#xff0c;在配置防火墙之前&#xff0c;我们需要先关闭firewall&#xff0c;安装iptables。 1.关闭firewall service firewalld stop systemctl disable …

mba学什么书_MBA的完整形式是什么?

mba学什么书MBA&#xff1a;工商管理硕士 (MBA: Master of Business Administration) MBA is an abbreviation of a Master of Business Administration. It is a masters degree for post-graduation in business administration. This business masters degree program is a …

Qt for Android 开发大坑

Qt for Android 开发大坑 作者: qyvlik Qt 5.5.1 这里说一说比較常见的 Qt 开发安卓的大坑。希望同学们不要做无谓的挣扎&#xff0c;跳过这些坑。输入框 首当其冲的是输入框&#xff0c;Qt 的输入在安卓上表现不佳. 无法支持安卓原生的输入法訪问 Qt 的输入框。就是安卓输入法…

bca ac如何联合索引_BCA的完整形式是什么?

bca ac如何联合索引BCA&#xff1a;计算机应用学士学位 (BCA: Bachelor of Computer Applications) BCA is an abbreviation of Bachelor of Computer Applications. It is a three-year undergraduate program in Computer applications. It is considered equivalent to B.Te…

path r'c test.html',robot framework - robot命令参数解析

robot 命令参数解析version > 3.0.1原文档查看命令:robot --helprobot -h-F --extension value通过文件扩展名控制需要执行的用例。如果只执行一个文件&#xff0c;这个参数无效。需要执行多个扩展名时&#xff0c;用“:”分隔开。Examples:--extension robot-F robot:txt-N…

嘿,程序员,你该学点经济学了!

前言&#xff1a; 笔者一直认为&#xff0c;一个好的程序员&#xff0c;不仅仅是代码敲得好&#xff0c;其它方面的知识和能力相同非常重要。特别是随着年龄的增长。非常多人也慢慢的往管理层发展。这个时候沟通与协调能力变得更加重要&#xff0c;而一些策划&#xff0c;推广方…

linux硬件配置_Linux硬件配置

linux硬件配置What sort of hardware configuration is expected to run Linux? This is a decent question; the real hardware configuration for the OS changes intermittently. The Linux Hardware−HOWTO gives a (pretty much) complete posting of hardware supported…

重邮2019计算机考研复试名单,重庆邮电大学2019年硕士研究生招生复试通知

当前2019年考研分数线已经公布&#xff0c;稳稳过线的同学即可全心准备复试了&#xff0c;中公考研小编整理了“重庆邮电大学2019年硕士研究生招生复试通知”文章&#xff0c;希望对大家有所帮助!各复试考生&#xff1a;根据《2019年重庆邮电大学硕士研究生复试工作方案》&…

Linux相关图解随记

01.dns解析过程02.用户访问网站流程03.局域网电脑上网流程04.网站架构图解转载于:https://blog.51cto.com/qinbin/1954149

数据库范式5nf_第五范式(5NF)| 数据库管理系统

数据库范式5nfFifth normal form (5NF) is also known as project-join normal form (PJ/NF). It is designed to minimize redundancy in relational databases by separating semantically connected relationships in multiple formats to store multi-valued facts. 第五范…

量子物理 詹班 计算机,(电气系计算机系詹班)量子物理作业答案

西南交大峨眉校区大学物理西南交大峨眉校区《大学物理》(量子物理基础)作业6(电气、计算机、詹班)一 选择题1. 以一定频率的单色光照射在某种金属上&#xff0c;测出其光电流曲线在图中用实线表示&#xff0c;然后保持光的频率不变&#xff0c;增大照射光的强度&#xff0c;测出…