Ok so I am working on a spring boot application which uses java, JSP and MySQL database. Originally the Spring Starter Security posed issues such as not showing my own login/signup pages, authentication errors etc. Because of this, I removed the Security Config file, and my project was working fine.
Then I made changes to the project so that the Security Config file will work fine since authentication and security is necessary for a web application. Everything was working fine, the whole project was running perfectly. A week ago, I formatted my C Drive because I bought a new SSD, but the original drive did not hold the IDE or the Project files. This resulted in my web application throwing “Forbidden 403 Error” from the login page itself.
After going mad for the better part of the day, I just commented out the Security Config File and the Starter Security dependency, and voila, the project is running smoothly again.
Can someone please tell me why Spring security is so cumbersome. I have read other threads, but could not find a good answer. Or if I am doing something wrong?
SecurityConfig.java
package com.example.wealthmanager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import com.example.wealthmanager.entity.User;
import com.example.wealthmanager.service.UserServ;
@Configuration
public class SecurityConfig {
@Bean
UserDetailsService userDetailsService(UserServ userService) {
return email -> {
User user = userService.findByEmail(email);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return org.springframework.security.core.userdetails.User
.withUsername(user.getEmail())
.password(user.getPassword())
.roles("USER")
.build();
};
}
@SuppressWarnings({ "deprecation", "removal" })
@Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(requests ->
requests.requestMatchers(HttpMethod.POST, "/login").permitAll()
.anyRequest().permitAll());
http.csrf(withDefaults());
return http.build();
}
}
application.properties
spring.application.name=demo-1
#Database Connection
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/wealthdb
spring.datasource.username=root
spring.datasource.password=password
#Hibernate Config
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
#spring.data.jdbc.repositories.enabled=false
#JPA Properties
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false
#View Resolver
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
If anything else is required I’ll be glad to provide it. Thanks in advance.
(PS it’s my first time posting on stack overflow.)
Kushagra Srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.