Springboot Oauth2单点登录实践

最近把网站的用户认证部分改成springboot oauth2完成,结合jwt基本实现了后台和前台完全分离,但是实现单点登录时还是遇到了很多的坑,在此记录一下,供大家参考。

单点登录认证服务器实现

这部分的内容很多地方都可以查得到,在此我就不做详细介绍了,如果不明白的,建议参考江南一点雨的系列教程,写得非常好!

https://mp.weixin.qq.com/s/AELXf1nmpWbYE3NINpLDRg

单点登录客户端实现

1、首先在pom.xml文件中引用oauth2依赖,加了这个依赖后就不用再加security了。


    org.springframework.cloud
    spring-cloud-starter-oauth2

2、加入SecurityConfig类,主要是加入@EnableOAuth2Sso注解,代码如下:

package com.fitit100.geodata.config;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().cors().disable();
    }

    /**
     * 需要忽略的静态资源
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**",
                "/images/**",
                "/css/**",
                "/pages/**",
                "/plugins/**",
                "/scss/**",
                "/geodata/**");
    }
}

3、修改配置文件。

szzgj:
  auth-server: http://auth.xxx.com #认证服务器的地址
security:
  oauth2:
    client:
      client-id: clientId
      client-secret: clientSecret
      user-authorization-uri: ${szzgj.auth-server}/oauth/authorize
      access-token-uri: ${szzgj.auth-server}/oauth/token
    resource:
      jwt:
        key-uri: ${szzgj.auth-server}/oauth/token_key
        key-value: szzgj-auth #这个一定要设置,而且跟认证端的JWT密码一致,否则无法解析用户信息,很多教程里面都没有写这个
server:
  servlet:
    session:
      cookie:
        name: OAUTH2SESSION-GEODATA #这个也一定要设置
#如果用Apache或Nginx做代理,这个也一定要设置,否则登录时调转的路径会不对  
tomcat:
    remoteip:
      host-header: "X-Forwarded-For"
      protocol-header: "X-Forwarded-Proto"
      protocol-header-https-value: "https"

如果用Apache做代理,那么Apache中需要做如下设置,这里我用两个tomcat做负载均衡,如果没有做负载均衡,直接做转发即可:


    ServerName cleint.xxx.com
    ServerAdmin [email protected]
    DirectoryIndex index.html index.jsp index.htm index.php
    #RequestHeader set X-Forwarded-Proto https
    #RequestHeader set X-Forwarded-Port 443
    ProxyPreserveHost On
    ProxyPass / balancer://ClientDemo/ stickysession=jsessionid nofailover=On
    ProxyPassReverse / balancer://ClientDemo/ 
    ProxyRequests Off
     
        BalancerMember http://192.168.70.69:8095 loadfactor=1 route=tomcat1
        #BalancerMember http://192.168.70.149:8095 loadfactor=1 route=tomcat2
    

你可能感兴趣的:(spring)