Answer the question
In order to leave comments, you need to log in
How to fix error 401 when authorizing in the application?
Good afternoon.
Here is my SecurityConfiguration :
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final DataSource dataSource;
public SecurityConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic();
http
.authorizeRequests()
.antMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-ui/**").permitAll()
.anyRequest().authenticated();
http.csrf().disable();
http.headers().frameOptions().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery("select id, name, password, activate from users where name=?")
.authoritiesByUsernameQuery("select users.name, roles.name from users_roles\n" +
"join roles on roles.id = users_roles.role_id\n" +
"join users on users.id = users_roles.user_id\n" +
"where users.name = ?");
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:postgresql://localhost:5432/bd
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.postgresql.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
#spring.jpa.hibernate.ddl-auto=create-drop
# Naming strategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
# ===============================
# = FLYWAY
# ===============================
spring.flyway.locations=classpath:db/migration
#spring.flyway.schemas=schema1
#spring.flyway.baseline-on-migrate=true
Answer the question
In order to leave comments, you need to log in
Good evening.
4xx errors are errors on the client.
It would be nice to see the content of the post request that goes to the server and of course what comes to the server.
The reasons may be different:
1) the content of the form (the names of the inputs - username and password), as well as the expected names of the inputs are different. For example, the server expects username & password and you send login & pass. Pay attention to the name attribute of the inputs and, if necessary, add the appropriate names to the config. .formLogin()
https://www.baeldung.com/spring-security-login
2) the request may not even reach the spring for some reason.
In general, I will not guess and you do not guess. Debug the project and everything will fall into place
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question