我使用 Spring Boot Security 作为我的 Restful 服务的 ACL。 安全适配器如下
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableRedisHttpSession
@Order(2)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailsService userDetailsService;
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and().csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and().userDetailsService(userDetailsService);
}
}
userdetailservice 的快照
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Yuangong yuangong = yuangongService.getYuangongByNo(username).getData();
List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<SimpleGrantedAuthority>();
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ALL"));
return new User(yuangong.getNo(), yuangong.getPassword(), grantedAuthorities);
}
@RestController注解的端点,以及端点中的方法如
@RestController
@RequestMapping(path = "/bumen")
public class BumenEndpoint {
// @PermitAll
@PreAuthorize("hasRole('ROLE_ALL')")
@RequestMapping(path = "/getBumenTreeList", method = RequestMethod.GET )
public HttpResult<List<Map<String, Object>>> getBumenTreeData(Principal principal) {
System.out.println(principal.getName());
return new HttpResult(bumenService.getBumenTreeList());
}
如果我使用 @permitAll,它可以找到并返回正确的 JSON 响应。如果使用 @PreAuthorize("hasRole('ROLE_ALL')"),它可以通过身份验证并可以调试到此方法,但响应将被重定向到“/bumen/bumen/getBumenTreeList”(双 '/bumen') 404 错误。 如果我不实现 BumenEndpoint,将不会被重定向并返回正确的响应。
我不确定哪个部分导致重定向。
最佳答案
该问题是由注释引起的。我已按照此修复它 Spring-MVC Problem using @Controller on controller implementing an interface
关于spring-security 重定向 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328320/