【问题标题】:Spring Custom Security With MySQL And JPA Giving 403 Access Denied使用 MySQL 和 JPA 提供 403 访问被拒绝的 Spring 自定义安全性
【发布时间】:2021-01-19 00:27:43
【问题描述】:

我正在尝试通过使用 UserDetailsS​​ervice 提供身份验证来访问我在邮递员上的 rest api,但每次我在每次请求时都发出 403 Access Denied 的请求。 POST 和 GET 方法的行为相同。我已经阅读了论坛上记录的其他问题,但每个答案都说这是由于 CSRF,我禁用了它,但问题仍然存在。

完整代码开启:https://github.com/afulz29/spring-security-demo.git

请帮助我,3 天以来我一直在努力解决这个问题。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{

@Autowired
UserDetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(userDetailsService)
        .passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http
        .authorizeRequests()
            .antMatchers("/api/**").authenticated().anyRequest().hasAnyRole("ADMIN");
}

@Bean
public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedMethods("*");
}
}



@RestController
@RequestMapping("/api")
public class UserController {

@Autowired
private UserService userService;

@GetMapping(path = "/users")
public User getUserById(@RequestParam("userId") Integer userId) {
    return userService.getUserById(userId);
}

@PostMapping(path = "/users", consumes = MediaType.APPLICATION_JSON_VALUE)
public User addUser(@RequestBody User user) {
    return userService.addUser(user);
}
}

【问题讨论】:

  • 你在控制台打印了什么吗?这似乎不是春季安全问题。如果安全不允许,spring 通常会返回 401
  • 控制台上没有错误,请您帮助理解代码中的错误。我已经提供了有问题的 git 存储库 url。
  • 我下载了repo。但无法在我的本地导入。似乎构建也有一些问题。让我再试一次
  • 我仍然无法加载您的项目并运行。遇到一些配置问题。如果我成功了会更新。
  • 好的。谢谢你。如果您发现不正确的地方,请告诉我。

标签: spring-boot spring-security spring-data-jpa spring-security-rest


【解决方案1】:

我发现您的安全配置存在一些问题:

  1. 基本身份验证未启用,但您尝试在邮递员中进行基本身份验证

执行以下操作以启用基本身份验证

        http
            .authorizeRequests()
                ...
                .and()
                .httpBasic();
  1. 我猜 POST /api/users 是一个用户注册端点。您必须将此端点列入白名单,以便任何人都可以注册
        http
            .authorizeRequests()
                    .antMatchers( HttpMethod.POST,"/api/users").permitAll()
                    .antMatchers("/api/**").authenticated()
                    .anyRequest().hasAnyRole("ADMIN")
                .and()
                    .httpBasic();

测试:

创建用户

POST: localhost:8080/api/users

{
        "userName" : "user1",
        "password": "pass"
}

获取用户信息


GET: localhost:8080/api/users?userId=1   //use the correct ID

With Basic Auth: userName = user1, password = pass

奖励反馈:

  1. User.userName --> 你可能想让这个字段唯一
  2. @Repository您的存储库接口中不需要此注释
  3. 用户服务接口。我看不出有任何理由使用接口和实现。

【讨论】:

  • 谢谢,我已按照建议进行了更改,代码现在按预期工作。
猜你喜欢
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 2012-07-16
  • 2015-09-07
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多