jHipster微服务搭建遇到的问题
jhipster简单来说是一个基于nodejs+yeoman的java代码生成器。往大了说是基于java的一套微服务解决方案。请注意是一整套的微服务解决方案。jhipster在整个程序架构上都做好了整合,包括前端mvvm框架(angularjs),前端构建工具(gulp)到后端的微服务框架(spring cloud)和hibernate/mongodb,再到单元测试/ui测试。
OAuth2相关
利用jHipster生成uaa(即: OAuth2)
1.配置充许访问的路径(即:无需鉴权)
找到 UaaConfiguration 类中的 configure(HttpSecurity http) 方法,添加无需鉴权的地址标识,如:
在其中添加 .antMatchers("/api/public/**").permitAll() 表示访问以 /api/public 开头的地址都无需鉴权,可直接访问
2.允许client使用form的方式进行authentication的授权
若通过 http://{{OAuth2}}/oauth/token 获取TOKEN,报 401 Unauthorized,则表示需要授权方可访问,如:
找到 UaaConfiguration 类中的 configure(AuthorizationServerSecurityConfigurer oauthServer) 方法,如:
在其中添加 oauthServer.allowFormAuthenticationForClients(); 即可
3.修改登录接口(及TOKEN刷新)
找到 UserResource 类中的 login(@Valid @RequestBody ManagedUserVM userVM) 方法,如:
注意这里登录接口的访问地址 /api/public/userManagement/login 其中加了 /api/public 所以这个接口是无需鉴权的
1: findOneByLoginAndPassword() 方法需要在 userRepository 自行定义
2: accessToken() 方法参考下面代码
以下是 userService 相关代码:
找到 UaaConfiguration 类,需要在类中重新注入几个 Bean 如:
Gateway相关
1.若通过网关调用服务,且访问路径包含 /api/public 则无需鉴权,需要修改以下两个地方:
在 OAuth2AuthenticationConfiguration 和 MicroserviceSecurityConfiguration 类中找到 configure(HttpSecurity http) 方法,如下:
OAuth2AuthenticationConfiguration类:
MicroserviceSecurityConfiguration 类:
在这两个类中添加 .antMatchers("/api/public/**").permitAll() 和 .antMatchers("/**/api/public/**").permitAll()