当前位置: 首页 > 知识库问答 >
问题:

API Spring Boot自动重定向到登录页面

魏威
2023-03-14

我正在尝试将Spring Security性与spring boot restful API集成。我的项目代码如下:

web安全配置包括

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().ignoringAntMatchers("/API/**");
        http.authorizeRequests()
        .antMatchers("/API/user/**").permitAll()
        .and().exceptionHandling().accessDeniedPage("/access-denied");

    }
}
@RestController
@RequestMapping("/API/user")
public class UserRestApiController {

    @Autowired
    UserService userService;

    @RequestMapping(value="/all", method=RequestMethod.GET)
    public ResponseEntity<Collection<Users>> getAll(){
        return new ResponseEntity<Collection<Users>>(userService.getAlluser(), HttpStatus.OK);
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public ResponseEntity<Users> getUser(@PathVariable(value="id") int id){
        return new ResponseEntity<Users>(userService.getUser(id), HttpStatus.OK);
    }

    @RequestMapping(value="/delete/{id}", method=RequestMethod.PUT)
    public String deleteUser(@PathVariable(value="id")int id){
        userService.deleteUser(id);
        return "DELETED";
    }

    @RequestMapping(value="/create", method=RequestMethod.POST)
    public ResponseEntity<String> addUser(@Valid @RequestBody Users user){
        return new ResponseEntity<String>(userService.addUser(user), HttpStatus.OK);
    }

    @RequestMapping(value="/update", method=RequestMethod.PUT)
    public ResponseEntity<Object> updateUser(@Valid @RequestBody Users user){
        return new ResponseEntity<Object>(userService.updateUser(user), HttpStatus.OK);
    }
}
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public Collection<Users> getAlluser() {
        return userRepository.findAll();
    }

    public Users getUser(int id) {
        return userRepository.getOne(id);
    }

    public void deleteUser(int id) {
        userRepository.deleteById(id);
    }

    public Users updateUser(Users user) {
        //Users u = userRepository.getOne(user.getId());
        Users userUpdate = null;
        if(userRepository.findById(user.getId()) != null) {
             userUpdate = userRepository.save(user);
        }
        return userUpdate;
    }

    public String addUser(Users user) {
        Users tmpUser = userRepository.findByUsername(user.getUsername());
        if(tmpUser == null) {
            userRepository.save(user);
            return "CREATED";
        } else {
            return "EXISTED";
        }
    }
}

你能帮我清理这个案子吗?

共有1个答案

阴培
2023-03-14

与Spring MVC不同,REST服务不应该将用户重定向到登录页面,因为它是一种资源,而不是视图解析器。相反,调用方(例如web浏览器)在收到HTTP代码401时将用户重定向到登录页面。

 类似资料:
  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我现在一直在努力使用我的登录页面来让组件呈现Loggedin组件。我的前端是Reactjs,后端是NodeJS。我对nodejs、expression和react都是新手。 在loginform组件上,我使用fetch进行了一次post,它将用户名和密码传递给后端的相应endpoint。没问题。在后端,它读取我存储用户(不使用任何数据库)的jsonfile来查找匹配项,如果用户名和密码都匹配,则它

  • 我在Laravel框架中的HomeController.php中有一段代码,但是我无法访问它重定向到登录页面的索引/主页。我犯了什么大错?

  • 问题内容: 我有一个网站,有一个用户系统。我想将wordpress的用户系统集成到该网站的系统中,但是我仍然想使用该网站的注册/登录页面。我不希望任何人都能使用Wordpress的登录或注册表格进行登录或注册。相反,当他们尝试访问Wordpress中的登录/注册页面时,我希望这些页面将它们重定向到我自己的登录/注册页面。 有什么办法吗?我已经尝试过Google,但是我所能找到的只是在用户登录或注册

  • 当(在Spring Security/MVC中)页面访问被拒绝时,由于用户没有足够的权限(尽管他已通过身份验证),我需要通过显示登录页面(而不是显示403拒绝访问页面的标准行为)提供作为另一个用户登录 我可以编写一个,重定向到登录页面。但是,当Spring Security发现有另一个用户登录时,它会做出怎样的反应?当新用户已成功通过身份验证时,我能否以某种方式注销旧用户?

  • 我使用的是Spring Security 4.1.1,但我遇到了一个问题:我试图访问URL,应用程序会重定向到登录页面。到现在为止,一直都还不错。 但是,成功登录后,应用程序会再次将我重定向到登录页面,并且不会创建任何会话,因此即使尝试直接访问URL(在URL栏中键入),应用程序也会重定向到登录页面。 有一些URL我必须要求登录才能访问它们。其他的,我可以访问无需身份验证。这些我不需要验证的URL