10. 10
We need to implement UserDetails interface
Registration - User
@Entity
public class User implements UserDetails {
private String username;
private String password;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;
private Set<Role> authorities;
}
User.java
11. 11
We need to implement GrantedAuthority interface
Registration - Roles
public class Role implements GrantedAuthority {
private String authority;
}
Role.java
Role Interface
12. 12
We need to implement UserDetailsService interface
Registration - UserService
@Service
public class UserServiceImpl implements UserDetailsService {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public void register(RegisterModel registerModel) {
bCryptPasswordEncoder.encode(password));
}
}
UserServiceImpl.java
Encrypt Password
13. 13
We need to disable CSRF protection temporally
Registration - Configuration
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.and()
.csrf().disable();
}
SecurityConfiguration.java
Disable CSRF
20. 20
This is the currently logged user
Principal
@GetMapping("/user")
public String getUser(Principal principal){
System.out.println(principal.getName());
return "user";
}
UserController.java
Print Logged-In
username
21. 21
Grant Access to specific methods
Pre/Post Authorize
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends
WebSecurityConfigurerAdapter {
}
SecurityConfiguration.java
public interface UserService extends UserDetailsService {
@PreAuthorize("hasRole('ADMIN')")
void delete();
}
UserService.java
Enables
PreAuthorize
Requires Admin
Role to execute
26. 26
Functionality to display data based on authentication rules
Thymeleaf Security
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
pom.xml
29. 29
Spring Security – framework that focuses
on providing both authentication
and authorization
Thymeleaf Security– functionality to display
data based on authentication rules
Summary