Я пытался разработать приложение, которое будет загружать URL-адреса авторизации из базы данных. Это приложение не должно иметь пользовательского интерфейса. Когда я пытаюсь отправить запрос через почтальона, я получаю ошибку «403». Если я добавлю .antMatchers("/login").permitAll();
вверху, то приложение разрешит все запросы, которые успешно аутентифицированы, независимо от того, есть ли у пользователя это разрешение или нет. Ниже приведены мои коды
WebSecurityConfig.cass
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/login").permitAll();
//Module List are holding modules which shall provide the URL's and authority names
List<Module> moduleList = _moduleDAO.getModule();
for (Module module : moduleList) {
if(module.getUrl() != null & module.getModuleName() != null) {
String url = module.getUrl();
String authority = module.getModuleName();
String requestType = module.getRequestType();
if(requestType != null) {
if(requestType == "GET") {
http.authorizeRequests().antMatchers(HttpMethod.GET,url).hasAuthority(authority);
}else if(requestType == "POST") {
http.authorizeRequests().antMatchers(HttpMethod.POST, url).hasAuthority(authority);
}else if(requestType == "DELETE") {
http.authorizeRequests().antMatchers(HttpMethod.DELETE,url).hasAuthority(authority);
}else if(requestType == "PUT") {
http.authorizeRequests().antMatchers(HttpMethod.PUT, url).hasAuthority(authority);
}
}
logger.info("-- Adding URL : " +url +" Request Type : "+requestType+" With Authority : " + authority);
}
}
http
.authorizeRequests()
.anyRequest().denyAll().and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login");
}
UserPrinciple.class
public class UserPrinciple implements UserDetails{
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
//Get Roles
if(this.roleList != null) {
this.roleList.stream().distinct().forEach(role -> {
if(role != null && role.getRole() != null) {
String RoleName = "ROLE_"+role.getRole();
logger.info("#Adding "+RoleName + " to the authority");
GrantedAuthority authority = new SimpleGrantedAuthority(RoleName);
authorities.add(authority);
}
});
//Get Modules
if(this.moduleList != null) {
this.moduleList.forEach(module ->{
String moduleName = module.getModuleName();
logger.info("#Adding "+moduleName + " to the authority");
GrantedAuthority authority = new SimpleGrantedAuthority(moduleName);
authorities.add(authority);
});
}
}
return authorities;
}
В базе данных I сохранили эти модули, как описано по ссылке ниже: https://pasteboard.co/JdHgh5W.png