| @@ -23,7 +23,6 @@ dependencies { | |||||
| implementation 'org.springframework.boot:spring-boot-starter-web' | implementation 'org.springframework.boot:spring-boot-starter-web' | ||||
| implementation 'org.springframework.boot:spring-boot-starter-validation' | implementation 'org.springframework.boot:spring-boot-starter-validation' | ||||
| implementation 'org.springframework.boot:spring-boot-starter-log4j2' | implementation 'org.springframework.boot:spring-boot-starter-log4j2' | ||||
| implementation 'org.springframework.security:spring-security-ldap' | |||||
| implementation 'org.liquibase:liquibase-core' | implementation 'org.liquibase:liquibase-core' | ||||
| implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' | implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' | ||||
| @@ -48,7 +47,6 @@ dependencies { | |||||
| compileOnly group: 'jakarta.servlet', name: 'jakarta.servlet-api', version: '6.0.0' | compileOnly group: 'jakarta.servlet', name: 'jakarta.servlet-api', version: '6.0.0' | ||||
| runtimeOnly 'com.mysql:mysql-connector-j' | runtimeOnly 'com.mysql:mysql-connector-j' | ||||
| runtimeOnly 'com.unboundid:unboundid-ldapsdk:6.0.9' | |||||
| testImplementation 'org.springframework.boot:spring-boot-starter-test' | testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||||
| testImplementation 'org.springframework.security:spring-security-test' | testImplementation 'org.springframework.security:spring-security-test' | ||||
| @@ -48,15 +48,6 @@ public class SecurityConfig { | |||||
| throws Exception { | throws Exception { | ||||
| return authenticationConfiguration.getAuthenticationManager(); | return authenticationConfiguration.getAuthenticationManager(); | ||||
| } | } | ||||
| @Bean | |||||
| @Qualifier("LdapAuthenticationManager") | |||||
| public AuthenticationManager ldapAuthenticationManager(BaseLdapPathContextSource contextSource) { | |||||
| LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource); | |||||
| factory.setUserSearchFilter("cn={0}"); | |||||
| return factory.createAuthenticationManager(); | |||||
| } | |||||
| @Bean | @Bean | ||||
| public PasswordEncoder passwordEncoder() { | public PasswordEncoder passwordEncoder() { | ||||
| return new BCryptPasswordEncoder(); | return new BCryptPasswordEncoder(); | ||||
| @@ -45,10 +45,6 @@ public class JwtAuthenticationController { | |||||
| @Qualifier("AuthenticationManager") | @Qualifier("AuthenticationManager") | ||||
| private AuthenticationManager authenticationManager; | private AuthenticationManager authenticationManager; | ||||
| @Autowired | |||||
| @Qualifier("LdapAuthenticationManager") | |||||
| private AuthenticationManager ldapAuthenticationManager; | |||||
| @Autowired | @Autowired | ||||
| private JwtTokenUtil jwtTokenUtil; | private JwtTokenUtil jwtTokenUtil; | ||||
| @@ -80,30 +76,11 @@ public class JwtAuthenticationController { | |||||
| return createAuthTokenResponse(authenticationRequest); | return createAuthTokenResponse(authenticationRequest); | ||||
| } | } | ||||
| @PostMapping("/ldap-login") | |||||
| public ResponseEntity<?> ldapLogin(@RequestBody JwtRequest authenticationRequest, HttpServletRequest request) throws Exception { | |||||
| String username = authenticationRequest.getUsername(); | |||||
| try { | |||||
| boolean success = ldapAuthenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword()); | |||||
| loginLogService.createLoginLog(username, request.getRemoteAddr(), success); | |||||
| } catch (Exception e) { | |||||
| loginLogService.createLoginLog(username, request.getRemoteAddr(), false); | |||||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | |||||
| .body(new ExceptionResponse("Unauthorized", ExceptionUtils.getStackTrace(e))); | |||||
| } | |||||
| return createAuthTokenResponse(authenticationRequest); | |||||
| } | |||||
| private boolean authenticate(String username, String password) throws Exception { | private boolean authenticate(String username, String password) throws Exception { | ||||
| authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); | authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); | ||||
| return true; | return true; | ||||
| } | } | ||||
| private boolean ldapAuthenticate(String username, String password) throws Exception { | |||||
| ldapAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); | |||||
| return true; | |||||
| } | |||||
| private ResponseEntity<?> createAuthTokenResponse(JwtRequest authenticationRequest) { | private ResponseEntity<?> createAuthTokenResponse(JwtRequest authenticationRequest) { | ||||
| final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername()); | final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername()); | ||||
| if (userDetails == null) { | if (userDetails == null) { | ||||
| @@ -1,5 +0,0 @@ | |||||
| spring: | |||||
| datasource: | |||||
| jdbc-url: jdbc:mysql://192.168.1.81:3306/arsdb?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8 | |||||
| username: root | |||||
| password: secret | |||||
| @@ -1,9 +0,0 @@ | |||||
| spring: | |||||
| ldap: | |||||
| embedded: | |||||
| port: 8389 | |||||
| base-dn: dc=springframework,dc=org | |||||
| ldif: classpath:ldap-test-users.ldif | |||||
| validation: | |||||
| enabled: false | |||||
| urls: ldap://localhost:8389 | |||||