Zabezpieczenie Spring Boot-Postman daje 401 nieautoryzowane
Rozwijam REST API w Spring Boot. Mogę wykonywać operacje CRUD, a listonosz udziela poprawnych odpowiedzi, ale kiedy dodam Spring Security, nazwa użytkownika i hasło Postman daje 401 nieautoryzowany.
Podałem nazwę użytkownika i hasło do zabezpieczenia rozruchu sprężynowego, jak pokazano poniżej.
application.proptries
spring.jpa.hibernate.ddl-auto=update
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/pal?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.security.user.name=root
spring.security.user.password=root
Zrobiłem podstawowe uwierzytelnienie z nazwą użytkownika root i hasłem superużytkownika.
Żądanie podglądu zawiera nagłówki pomyślnie zaktualizowanych postów:
https://i.stack.imgur.com/4fiqS.png
EDIT
Usunąłem pliki cookie w listonoszach, ale nadal mam ten sam problem
https://i.stack.imgur.com/bZYR6.png
SecurityConfing.java
My Security Configuration are as below.
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
@EnableWebSecurity
@Order(1000)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
public void configureGlobal(AuthenticationManagerBuilder authenticationMgr) throws Exception { authenticationMgr.jdbcAuthentication().dataSource(dataSource())
.usersByUsernameQuery( "select email,password from user where email=? and statusenable=true")
.authoritiesByUsernameQuery( "select email,role from user where email=? and statusenable=true"); System.out.println(authenticationMgr.jdbcAuthentication().dataSource(dataSource())
.usersByUsernameQuery( "select email,password from user where email=? and statusenable=true")
.authoritiesByUsernameQuery( "select email,role from user where email=? and statusenable=true"));
} @Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/pal");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("");
return driverManagerDataSource;
} @Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.authorizeRequests().antMatchers("/admin/**").hasAnyRole("ROLE_ADMIN","ROLE_USER").anyRequest().permitAll()
.and()
.authorizeRequests().antMatchers("/user/**").hasAnyRole("ROLE_USER").anyRequest().permitAll();}
Nie znaleziono powiązanych wyników
Zaproszony:
Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się
1 odpowiedzi
Anonimowy użytkownik
Potwierdzenie od:
Musisz skonfigurować Spring Security, domyślnie wszystkie trasy są zabezpieczone do autoryzacji.
Zobacz implementację tokena JWT w tym miejscu
połączyć
https://github.com/nishant-var ... ation
.