It is a library that makes Spring Security easy to use to spring boot 2.0
Simple Spring Security depends on Model Mapper, Project Lombok
<dependency>
<groupId>com.github.ckpoint</groupId>
<artifactId>simple-spring-security</artifactId>
<version>0.0.4</version>
</dependency>
compile group: 'com.github.ckpoint', name: 'simple-spring-security', version: '0.0.4'
- 1. Add Simple UserDetails
- 2. Add Simple Spring Security Service
- 3. Change Password Encoder (Optional)
- 4. Encode password
- 5. Add Login Controller
- 6. Properties
public class TestUserDetail extends SimpleUserDetails {
private Long id;
private String userName;
private String password;
private String role;
@Override
public String getPassword() {
return this.password;
}
@Override
public String getUsername() {
return this.userName;
}
@Override
protected String getRole() {
return this.role;
}
@Override
protected List<String> getRoles() {
return null;
}
}
Simply override the createPasswordEncoder function in the SimpleSecurityService and return the encoder you want to use.
@Service
@RequiredArgsConstructor
public class TestSecurityService extends SimpleSecurityService {
....
@Override
protected PasswordEncoder createPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
SimplePasswordEncoder.encodePassword("1234");
D. The csrf and cors filter are set through the overrides of the isUseCsrf and isUseCors functions, respectively.
@Service
@RequiredArgsConstructor
public class TestSecurityService extends SimpleSecurityService {
@NonNull
private final AccountService accountService;
@Override
public SimpleUserDetails loadUserByUsername(String userName) {
return new TestUserDetail().updateFromObj(accountService.findAccountFromUserName(userName));
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/public/**").permitAll()
.anyRequest().authenticated();
}
@Override
public boolean isUseCsrf() {
return false;
}
@Override
public boolean isUseCors() {
return true;
}
}
@RestController
@RequiredArgsConstructor
@RequestMapping("/public")
public class AccountController {
@NonNull
private final TestSecurityService testSecurityService;
@PostMapping("/login")
public AccountModel login(@RequestBody AccountModel accountModel, HttpSession httpSession) {
return this.testSecurityService.login(accountModel.getUserName(), accountModel.getPassword(), AccountModel.class, httpSession);
}
@GetMapping("/session")
public TestUserDetail sessionCheck(TestUserDetail testUserDetail) {
return testUserDetail;
}
}