【发布时间】:2020-11-16 18:59:17
【问题描述】:
我尝试在 Spring Boot 上为我的移动应用实现小型 API 网关。
在我的架构中,我将 MS Active Directory Server 用于公司的身份验证人员,并且将来将为客户公司发送短信验证代码以发送 JWT。
我不使用层 DAO、UsersRepository 和 DB 连接。
所有通过 RestTemplate 从服务层发送到我们内部 CRM 系统的 HTTP 请求。
我实现LDAP AD auth很简单HttpBasic配置如下:
@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and().csrf()
.disable()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/api/v1/send/**").permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider = new
ActiveDirectoryLdapAuthenticationProvider("mydomain.com", "ldap://192.168.0.100:389/");
activeDirectoryLdapAuthenticationProvider.setConvertSubErrorCodesToExceptions(true);
activeDirectoryLdapAuthenticationProvider.setUseAuthenticationRequestCredentials(true);
activeDirectoryLdapAuthenticationProvider.setSearchFilter("(&(objectClass=user)(userPrincipalName={0})(memberOf=CN=mobileaccess,OU=User Groups,OU=DomainAccountsUsers,DC=MYDOMAIN,DC=COM))");
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider);
auth.eraseCredentials(true);
}
}
例如,我有两个 RestController V1 和 V2:
@RequestMapping("api/v1")
//get token for staff (AD user) HttpBasic auth
@PostMapping("auth/get/stafftoken")
public ResponseEntity<?> getToken() {
// some code...
HttpHeaders tokenHeaders = new HttpHeaders();
tokenHeaders.setBearerAuth(tokenAuthenticationService.getToken());
return new ResponseEntity<>(tokenHeaders, HttpStatus.OK);
}
//get JWT if code from sms == code in my CRM-system (for client) not auth - permitAll
@PostMapping("send/clienttoken")
public @ResponseStatus
ResponseEntity<?> sendVerifyCode(@RequestParam("verifycode") String verifycode) {
// some code...
HttpHeaders tokenHeaders = new HttpHeaders();
tokenHeaders.setBearerAuth(tokenAuthenticationService.getToken());
return new ResponseEntity<>(tokenHeaders, HttpStatus.OK);
}
@RequestMapping("api/v2")
@GetMapping("get/contract/{number:[0-9]{6}")
public Contract getContract(@PathVariable String number) {
return contractsService.getContract(number);
}
如何使用 JWT 令牌(客户端和员工)实现对 Controller APIv2 的 Bearer Auth 请求?
我认为这是通过过滤器链实现的?
【问题讨论】:
标签: spring-boot spring-security spring-security-ldap