springcloud微服务项目架构搭建第二天:整合oauth2时遇到的坑

推荐文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
<http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html>

springboot2.0 oauth2重要变更
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#security

<https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#security>

1、在新版本中,如果要将Spring
Security的AuthenticationManager公开为bean,需要重写WebSecurityConfigurerAdapter
上的authenticationManagerBean方法,并使用@Bean注释它。

2、在springboot2中,security配置有了重大的改变,有兴趣的可以阅读原文
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#security

<https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#security>
,简单说一下就是Spring Boot 2大大简化了默认的安全配置,使添加自定义安全性变得简单。Spring
Boot现在只有一个添加自己的行为后退,而不是有几个与安全相关的自动配置WebSecurityConfigurerAdapter
。如果您使用以下任何属性,则会受到影响:

 
security.basic.authorize-mode security.basic.enabled security.basic.path
security.basic.realm security.enable-csrf security.headers.cache
security.headers.content-security-policy
security.headers.content-security-policy-mode security.headers.content-type
security.headers.frame security.headers.hsts security.headers.xss
security.ignored security.require-ssl security.sessions

 而且management.security.enabled配置被取消,被新的management.endpoints.web.exposure.include=
'*'取代,通过配置各个端点来达到安全控制的目的,当然,也可以通过继承WebSecurityConfigurerAdapter并重写configure(HttpSecurity
http)方法来配置RequestMatcher 属性达到目的

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      
 http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
                .anyRequest().hasRole("ENDPOINT_ADMIN")
                .and()
            .httpBasic();
    }

}


上面给出的是官方事例, 上面的示例用于EndpointRequest.toAnyEndpoint()将请求与任何端点进行匹配,然后确保所有端点都具有该ENDPOINT_ADMIN角色。其他几种匹配方法也可用EndpointRequest。有兴趣的可以阅读
https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/actuator-api//html/
<https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/actuator-api//html/>

3、在Spring5.0中,WebMvcConfigurerAdapter已经弃用,

替代类:WebMvcConfigurationSupport或者DelegatingWebMvcConfiguration)
extends WebMvcConfigurerAdapter+@EnableWebMvc 等同于 extends
WebMvcConfigurationSupport
切勿使用@EnableWebMvc和 extends WebMvcConfigurationSupport 在一起。


在继承WebMvcConfigurationSupport后发现自动配置的静态资源路径不生效,在查阅WebMvcAutoConfiguration源码后发现了一个神奇的事情



图中的@ConditionalOnMissingBean注解的意思是在项目类路径中 缺少
 WebMvcConfigurationSupport类型的bean时改自动配置类才会生效,所以继承 WebMvcConfigurationSupport
后需要自己再重写相应的方法。

而上面说过spring5.0WebMvcConfigurerAdapter已经弃用,所以后两种方法:


1)、实现WebMvcConfigurer使用默认路径【classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/】(推荐,简单)

2)、继承WebMvcConfigurationSupport,并重写addResourceHandlers方法进行自定义路径
/** * 配置静态资源路径. * * @param registry registry. */ @Override public void
addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
WebMvcConfigurer.super.addResourceHandlers(registry); }
4、重点来了,敲黑板 ,一个卡了好几天的神坑,关于security5.x中passwordEncode的改动

在5.x以前,security中的加密配置是酱紫的
@Bean public PasswordEncoder passwordEncoder() { return new
BCryptPasswordEncoder(); }
 这样配置的好处就是能快速的注入并bcrypt加密。

在5.x以后呢,在注入bean 的时候不能显式指定 PasswordEncoder 的实现类,类比旧方法。只能通过工厂类来创建
@Bean public PasswordEncoder passwordEncoder() { return
PasswordEncoderFactories.createDelegatingPasswordEncoder(); }
看一下源码




 通过工厂创建的bean默认也是使用bcypt格式,但是也可以通过自定义的方式创建自己加密类型,也就意味着同一张表里面可以存储不同加密格式的密码,那要怎么区分加密格式呢。。。。
{id}encodedPassword

id是用于查找PasswordEncoder应该使用的标识符,并且encodedPassword是所选的原始编码密码PasswordEncoder。在id必须在密码的开始,开始{和结束}。如果id找不到,id则为null。例如,以下可能是使用不同编码的密码列表id。所有原始密码都是“密码”。
以下例子来自官方文档:
{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG (1)
{noop}password(2)
{pbkdf2}5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc
(3)
{scrypt}$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=
(4)
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0
(5)
第一个密码的PasswordEncoderid为bcrypt和encodedPassword


$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG。匹配时会委托给BCryptPasswordEncoder

第二个密码的PasswordEncoderid为noop和encodedPassword
password。匹配时会委托给NoOpPasswordEncoder

第三个密码的PasswordEncoderid为pbkdf2和encodedPassword


5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc。匹配时会委托给Pbkdf2PasswordEncoder

第四个密码的PasswordEncoderid为scrypt和encodedPassword


$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=
匹配时它将委托给SCryptPasswordEncoder

最后的密码的PasswordEncoderid为sha256和encodedPassword


97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0。匹配时会委托给StandardPasswordEncoder



附上原文地址:
https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#migrating-to-spring-security-5

<https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#migrating-to-spring-security-5>

5、redis 2.2.x 版本中,当授权Auth-Server 配置token 保存在redis 时,报了下面的错误。

 原因:2.2.x版本中set(String,String)被弃用了,要使用RedisConnection.stringCommands().set(…)
  解决方法:
 1、不使用2.2.x 版本,升级到2.3.x
 2、重写tokenStore .因为最新版中RedisTokenStore的set已经被弃用了,
  所以我就只能自定义一个,代码和RedisTokenStore一样,
 只是把所有conn.set(…)都换成conn..stringCommands().set(…)

 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信