Spring Security 总是在postman中返回403禁止

3mpgtkmj  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(543)

我正在尝试配置spring安全授权,但每次 Postman 请求都会得到403(禁止)。
我检查了其他问题,但没有任何效果。任何人谁得到了这个问题,并解决了它分享我需要做什么来解决它?
我想添加一个授权,使 /authenticate 可供所有用户访问, /registeradmin , /registersimpleuser/listallusers 仅适用于管理员角色。
我的成绩是403平 /authenticate 配置为“允许所有”。
spring安全配置类:

package com.project.encheres.security.configuration;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import com.project.encheres.repository.UserRepository;

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@Configuration
@EnableWebSecurity
@Component
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final UserRepository userRepo;
    @Autowired
    UserDetailsService userDetailsService;
    public SecurityConfiguration(UserRepository userRepo) {
        this.userRepo = userRepo;
    }
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws     Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());       
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {  
        http
            .httpBasic()
                .and()
            .authorizeRequests()
                .antMatchers(HttpMethod.POST, "localhost:8070/user/registersimpleuser").permitAll();
        http
            .httpBasic()    
                .and()
            .authorizeRequests()
                .antMatchers(HttpMethod.POST, "localhost:8070/authenticate").permitAll()
                .antMatchers(HttpMethod.GET, "localhost:8070/user/listallusers").hasAuthority("ADMIN");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    public BCryptPasswordEncoder gePasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

goqiplq2

goqiplq21#

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .httpBasic()
            .and()
        .authorizeRequests()
            .antMatchers(HttpMethod.POST, "localhost:8070/user/registersimpleuser").permitAll();
    http
        .httpBasic()
            .and()
        .authorizeRequests()
            .antMatchers(HttpMethod.POST, "localhost:8070/authenticate").permitAll()
            .antMatchers(HttpMethod.GET, "localhost:8070/user/listallusers").hasAuthority("ADMIN");
    http
        .csrf().disable();
}

刚刚加上 http.csrf().disable(); 在方法的最后。
这将描述为什么您的代码之前不工作

相关问题