2016-12-26 163 views
2

我使用彈簧引導安全性作爲我的寧靜服務的ACL。 安全適配器如下彈簧安全重定向404錯誤

@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')」),它可以傳遞auth並且可以調試到這個方法,但是響應將被重定向到「/ bumen/bumen/getBumenTreeList」(double'/ bumen') 404錯誤。 如果我沒有實現BumenEndpoint,將不會被重定向並返回正確的響應。

我不確定哪個部分會導致重定向。

+0

什麼是'HttpResult'? – chaoluo

回答