Spring Cloud Gateway + Spring Security实现前后分离的LDAP登录验证

我们之前的做的微服务项目,网关鉴权选择的都是JWT方案。这次的项目因为一些原因,选择cookie的方式。从前端的角度看,从传统的前后分离登录方案迁移到现有微服务平台,不需要任何改动。

大致思路很简单,springcloud gateway结合spring security和ldap做登录验证,登录成功后,将用户基本信息,例如userId,userName,权限等解析出来,通过http header传给后端微服务。

主要的三个依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>

1. LDAP验证

首先配置ldap验证。ContextSource和LdapTemplate和传统项目方式一样,没什么特殊的地方,但是在webflux下,需要配置一下ReactiveAuthenticationManager才能实现ldap验证。

   @Bean
    ReactiveAuthenticationManager authenticationManager(
            BaseLdapPathContextSource contextSource,
            LdapCustomAuthoritiesPopulator populator,
            MoreUserDetailsContextMapper moreUserDetailsContextMapper) {

        BindAuthenticator ba = new BindAuthenticator(contextSource);
        ba.setUserDnPatterns(new String[] { "cn={0},ou=people" } );

        LdapCustomPrivilegeAuthenticationProvider lap = new LdapCustomPrivilegeAuthenticationProvider(ba, populator);
        // 出于性能的考虑,在LdapAuthoritiesPopulator里,获取权限的同时,check了用户是否在系统中存在,这个方法同时会
        // 获取用户的权限,为了少一次调用,扩展创建用户的方法,将权限和用户在本地系统的id等信息全部写入session
        lap.setUserDetailsContextMapper(moreUserDetailsContextMapper);
        AuthenticationManager am = new ProviderManager(Arrays.asList(lap));

        return new ReactiveAuthenticationManagerAdapter(am);
    }
    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        Map<String, Object> config = new HashMap();

        contextSource.setUrl("ldap://url.example.com:389/");
        contextSource.setBase("DC=example,DC=com");
        contextSource.setUserDn("xxx@example.com");
        contextSource.setPassword("123456");

        config.put("java.naming.ldap.attributes.binary", "objectGUID");

        contextSource.setPooled(false);
        contextSource.setBaseEnvironmentProperties(config);
        contextSource.afterPropertiesSet();
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return  new LdapTemplate(contextSource());
    }

LdapPrivilegeAuthenticationProvider使用了自定以的实现方式,主要因为前端传过来的密码经过AES加密,spring security不支持AES加密,所以需要自己实现。这里使用AES的原因是ldap进行用户名和密码的验证时在ldap服务端,而ldap服务器不支持加密的密码,需要我们程序将密码解密,通过明文传递。

public class LdapCustomPrivilegeAuthenticationProvider extends LdapAuthenticationProvider {
    public LdapCustomPrivilegeAuthenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authoritiesPopulator) {
        super(authenticator, authoritiesPopulator);
    }

    @Override
    protected DirContextOperations doAuthentication(UsernamePasswordAuthenticationToken authentication) {
        String password = (String) authentication.getCredentials();
        try {
            String[] ps = password.split("\\.");
            password = AesUtils.getInstance().decrypt(ps[0], ps[1]);
        } catch (Exception e) {
            throw new BadCredentialsException("密码错误!");
        }
        return super.doAuthentication(new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), password));
    }
}

LdapAuthoritiesPopulator和UserDetailsContextMapper也是自定义实现,前者用来获取用户权限,后者主要用来构建用户信息,框架原生的实现功能太简单了,不能适应需求。

@Component
public class LdapCustomAuthoritiesPopulator implements LdapAuthoritiesPopulator {
    @DubboReference
    private UserService userService;

    @Override
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations dirContextOperations, String username) {
        // 这个地方正常的操作应该是获取用户权限,但是这里需要一次rpc
        // 后面需要用户id和用户姓名的信息还要一次rpc,所以把获取权限的操作滞后
        // 放在MoreUserDetailsContextMapper里
        return null;
    }
}
@Component
public class MoreUserDetailsContextMapper extends LdapUserDetailsMapper {
    @Autowired
    private UserService userService;

    @Override
    public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
        UserTo user = userService.getUser(username);
        if (user == null || user.getUserId() == null) {
            throw new BadCredentialsException("用户不存在当前系统,请联系管理员!");
        }
        List<GrantedAuthority> ret = new ArrayList<>(user.getPrivileges().size());
        for (PrivilegeTo privilege : user.getPrivileges()) {
            SimpleGrantedAuthority sga = new SimpleGrantedAuthority(privilege.getPrivilegeCode());
            ret.add(sga);
        }

        UserDetailInfo udi = new UserDetailInfo();
        udi.setUserId(user.getUserId());
        udi.setUserNickName(user.getUserNickName());
        udi.setAuthorities(ret);

        return udi;
    }
}

MoreUserDetailsContextMapper 主要用来验证用户是否在当前系统中,因为ldap是整个企业的用户验证,ldap验证通过并不等于可以登录当前系统,需要管理员将指定人员加入当前系统才能登录。mapUserFromContext方法返回一个自定义的UserDetails对象,扩展了userId和权限等字段,后面网关会将信息转发至后台微服务。

至此,已经实现通过LDAP验证用户名密码功能。

2. 前后分离的Session验证

Spring Security默认情况下,检查到请求没有登录会重定向到框架里实现的登录页面,在前后分离的情况,都是通过ajax请求进行登录,最好还是以json的形式返回,所以需要自定义session失效,登录成功和登录失败的行为。

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        http.csrf().disable()
                .cors().configurationSource(urlBasedCorsConfigurationSource)
                .and()
                .authorizeExchange(exchanges -> exchanges
                        .anyExchange().authenticated()
                )
                .formLogin().loginPage("/api/login")
                .requiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/api/login"))
                .authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
                .authenticationSuccessHandler(new JsonAuthenticationSuccessHandler())
                .authenticationFailureHandler(new JsonAuthenticationFailureHandler()).and();
        return http.build();
    }

这里主要配置登录相关的自定义行为,跨域的代码也在这里。

public class JsonAuthenticationFailureHandler implements ServerAuthenticationFailureHandler {
    @Override
    public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
        ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
        response.setStatusCode(HttpStatus.OK);
        ResultBO ret = new ResultBO();
        ret.setSuccess(false);
        ret.setErrorMessage("用户名或密码错误!");

        String body = JSONObject.toJSONString(ret);
        DataBuffer buffer = null;
        try {
            buffer = response.bufferFactory().wrap(body.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return response.writeWith(Mono.just(buffer));
    }
}
public class JsonAuthenticationSuccessHandler implements ServerAuthenticationSuccessHandler {

    @Override
    public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
        ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
        response.setStatusCode(HttpStatus.OK);
        ResultBO ret = new ResultBO();
        String body = JSONObject.toJSONString(ret);
        DataBuffer buffer = null;
        try {
            buffer = response.bufferFactory().wrap(body.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return response.writeWith(Mono.just(buffer));
    }
}

至此,登录相关的功能已经完成。当请求没有登录信息的时候,返回Http 401给前端。

3. 流量转发

网关的主要功能之一就是流量转发,这里出来基本的转发功能外,还需要将session中的用户信息转发给后端微服务。

public class CookieAuthenticationFilter implements GlobalFilter, Ordered {

    private final DataBufferFactory factory = new DefaultDataBufferFactory();
    
    @Resource
    private ObjectMapper objectMapper;

    @DubboReference
    private LoginService loginService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        AtomicReference<String> userName = new AtomicReference<String>();
        AtomicReference<Integer> userId = new AtomicReference<Integer>();
        List<String> auth = new CopyOnWriteArrayList<String>();
        exchange.getSession().flatMap(webSession -> {
            SecurityContextImpl context = (SecurityContextImpl) webSession.getAttribute("SPRING_SECURITY_CONTEXT");
            UserDetailInfo principal =
                    (UserDetailInfo) context.getAuthentication().getPrincipal();
            return Mono.just(principal);
        }).subscribe(e -> {
            userName.set(e.getUserNickName());
            userId.set(e.getUserId());
            for (GrantedAuthority authority : e.getAuthorities()) {
                auth.add(authority.getAuthority());
            }
        });
        Gson gson = new Gson();

        //认证通过
        ServerHttpRequest request = exchange.getRequest().mutate()
                .header("userId",String.valueOf(userId.get()))
                .header("UserName", userName.get())
                .header("auth", gson.toJson(auth))
                .build();

        return chain.filter(
                exchange.mutate()
                        .request(request).build());

    }

    private Mono<Void> unauthorized(ServerWebExchange serverWebExchange, String message) {
        ServerHttpResponse response = serverWebExchange.getResponse();
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        response.setStatusCode(HttpStatus.UNAUTHORIZED);

        return serverWebExchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return -200;
    }

}

4. 跨域问题

见第2部分。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容