После применения Spring Security в моем проекте контроллер не работает, когда я вызываю остальные контроллеры, это просто ответ с кодом 404.
Это мой класс конфигурации безопасности Spring
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().permitAll();
}
}
Файл My Application.properties
spring.datasource.url = jdbc:mysql://192.168.1.62:3306/dummy_users?useSSL=false
spring.datasource.username = root
spring.datasource.password = MySQL62$$
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
Класс My Initial Data Loader
package com.dummy.users.auth.config;
import com.dummy.users.auth.entity.Privilege;
import com.dummy.users.auth.entity.UsersProfileEntity;
import com.dummy.users.auth.entity.UsersRoles;
import com.dummy.users.auth.repository.PrivilegeRepository;
import com.dummy.users.auth.repository.UserProfileRepository;
import com.dummy.users.auth.repository.UsersRolesRepository;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {
boolean alreadySetup = false;
@Autowired
private UserProfileRepository userProfileRepository;
@Autowired
private UsersRolesRepository usersRolesRepository;
@Autowired
private PrivilegeRepository privilegeRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
if (alreadySetup) {
return;
}
Privilege readPrivilege
= createPrivilegeIfNotFound("READ_PRIVILEGE");
Privilege writePrivilege
= createPrivilegeIfNotFound("WRITE_PRIVILEGE");
List<Privilege> adminPrivileges = Arrays.asList(
readPrivilege, writePrivilege);
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
createRoleIfNotFound("ROLE_USER", Arrays.asList(readPrivilege));
// UsersRoles adminRole = usersRolesRepository.findByName("ROLE_ADMIN");
// UsersProfileEntity user = new UsersProfileEntity();
// user.setFirstName("KarthickRaj");
// user.setLastName("Rathinakumar");
// user.setPhoneNumber("95245356782");
// user.setPassword(passwordEncoder.encode("2342423$$#"));
// user.setEmailId("rkarthickraj@gmail.com");
// user.setStatus("Active");
// user.setRoles(Arrays.asList(adminRole));
// user.setEnabled(true);
// userProfileRepository.save(user);
alreadySetup = true;
}
@Transactional
private Privilege createPrivilegeIfNotFound(String name) {
Privilege privilege = privilegeRepository.findByName(name);
if (privilege == null) {
privilege = new Privilege();
privilege.setName(name);
privilegeRepository.save(privilege);
}
return privilege;
}
@Transactional
private UsersRoles createRoleIfNotFound(
String name, Collection<Privilege> privileges) {
UsersRoles role = usersRolesRepository.findByName(name);
if (role == null) {
role = new UsersRoles();
role.setName(name);
role.setPrivileges(privileges);
usersRolesRepository.save(role);
}
return role;
}
}
MyApplication.class для моего приложения
package com.dummy.users;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class UsersApplication {
private static final Logger LOGGER = LogManager.getLogger(UsersApplication.class.getName());
public static void main(String[] args) {
SpringApplication.run(UsersApplication.class, args);
}
}
Класс MyController, когда я получаю доступконтроллер это ответ 404 выпуск
@RestController
public class UsersLinkAdminController {
@GetMapping("/")
public String getUsersStatus() {
return "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>\n"
+ "<title>Page Title</title>\n"
+ "<style>\n"
+ "body {\n"
+ " background-color: black;\n"
+ " text-align: center;\n"
+ " color: white;\n"
+ " font-family: Arial, Helvetica, sans-serif;\n"
+ "}\n"
+ "</style>\n"
+ "</head>\n"
+ "<body>\n"
+ "\n"
+ "<h1>Welcome to Dummy Oraganization</h1>\n"
+ "<p>Users Module Running Sucessfully</p>\n"
+ "<p>For More Details Call Support team</p>\n"
+ "<img src=\"avatar.png\" alt=\"Avatar\" style=\"width:200px\">\n"
+ "\n"
+ "</body>\n"
+ "</html>";
}
}
Так что кто-нибудь Пожалуйста, сообщите мне о проблемах.