the simplest way is when your roles are not change.for example you have 7 part in your system, admin panel(just for see the panel and do nothing), users etc. like below:
http.csrf().disable()
.authorizeRequests().antMatchers("/","/css/**","/js/**","/img/**","/loginUser/**","/users/**","/webfonts/**",
"/productsList/**","/collectionList/**","/artists/**"
,"/loginCllients"
).permitAll()
.antMatchers("/admin").hasAuthority("1")
.antMatchers("/admin/users/**").hasAuthority("2")
.antMatchers("/admin/products/**").hasAuthority("3")
.antMatchers("/admin/collections/**").hasAuthority("4")
.antMatchers("/admin/categoryies/**").hasAuthority("5")
.antMatchers("/admin/authorities/**").hasAuthority("6")
also have this below code:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.passwordEncoder(new BCryptPasswordEncoder())
.usersByUsernameQuery("select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery("select username,authorities from authorities where username = ?");
}
now you should create an table for this permissions like below:
private int id;
private String username;
private String authorities;
public Authorities() {
super();
}
public Authorities(int id, String username, String authorities) {
super();
this.id = id;
this.username = username;
this.authorities = authorities;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthorities() {
return authorities;
}
public void setAuthorities(String authorities) {
this.authorities = authorities;
}
}
and put some code for insert role for every username. for example admin username have 5 role. roles 1,2,3,4,5
notice : if you want to dynamic roles the approach were be different.
I hope it is helpfull.