lem*_*des 6 java spring spring-security spring-ldap jwt
您好,我一直在尝试配置 spring,使其在用户/密码通过 LDAP 服务器身份验证时返回 JWT 令牌;考虑下面的用例;
在上图中,我已将 WebSecurity 配置为使用 Bearer 检查/过滤请求。见下面的代码
网络安全配置文件
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
@Autowired
JwtAuthorizationTokenFilter authenticationTokenFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure Web Security
// Allow only /auth/
// Disallow all others
http
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST,
"/auth/**")
.permitAll()
.anyRequest().authenticated();
//Custom JWT
http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// disable page caching
http.headers().cacheControl();
}
}
Run Code Online (Sandbox Code Playgroud)
验证控件.java
@RestController
@RequestMapping("auth")
public class AuthCtrl {
private static final Logger logger = LoggerFactory.getLogger(AuthCtrl.class);
@Autowired
@Qualifier("authenticationManagerImpl")
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
@Qualifier("userDetailsServiceImpl")
private UserDetailsService userDetailsService;
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String post(@RequestBody Map<String, String> credentials) {
logger.info("POST: {} | {} ",credentials.get("username"), credentials.get("password"));
String username = credentials.get("username");
String password = credentials.get("password");
Objects.requireNonNull(username);
Objects.requireNonNull(password);
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
// Reload password post-security so we can generate the token
final UserDetails userDetails = userDetailsService.loadUserByUsername(username);
final String token = jwtTokenUtil.generateToken(userDetails);
return token;
} catch (DisabledException e) {
throw new AuthenticationException("User is disabled!", e);
} catch (BadCredentialsException e) {
throw new AuthenticationException("Bad credentials!", e);
}
}
@ExceptionHandler({AuthenticationException.class})
public ResponseEntity<String> handleAuthenticationException(AuthenticationException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
上面的配置基于我见过的youtube指南,也是从git 中的演示源中提取的。大有帮助!,感谢业主。必须了解过滤器的工作原理。
上述来源已经可以过滤掉所有受保护的 API,并在未经授权时将未经授权的 API 作为响应发送回来。我允许匿名访问的唯一 api 是身份验证 api /auth
。它已经可以接收请求并通过网络过滤器。
但是我不太清楚如何对 LDAP 服务器的上述请求进行身份验证并发送 JWT 令牌。在我读过的指南中,他们正在获取有关数据库的用户信息。
我在 WebConfiguration 中阅读了一些关于 LDAP 配置的文档,但我无法将它与我当前的过滤器相关联。
请查看下面的链接,我使用 spring 4 创建了它。
而不是类路径上的 .ldif 文件配置您自己的 ldap 服务器。
https://github.com/merugu/springsecurity/tree/master/ldapauthenticationjwttoken
唯一的区别是对于 Spring 5,你应该使用像 Bcryptpasswordencoder 这样的高级密码编码算法。因为 LDAPpasswordEncoder 已被弃用。
快乐编码!
归档时间: |
|
查看次数: |
14240 次 |
最近记录: |