小编Vũ *_*ơng的帖子

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

我正在尝试将 Spring Security 与 Spring Boot Restful API 集成。我的项目的代码如下:

网络安全配置是

@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");

    }
}
Run Code Online (Sandbox Code Playgroud)

用户控制器:

@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) …
Run Code Online (Sandbox Code Playgroud)

java api spring spring-boot

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

api ×1

java ×1

spring ×1

spring-boot ×1