2015-11-02 50 views
0

我嘗試在後端實現Spring Security,並在前端Web應用程序上使用Angularjs一起工作。但是,當我嘗試通過/用戶的方法我得到了驗證用戶:使用Spring安全主體對象進行身份驗證時Spring RestController的IllegalArgumentException

java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.security.authentication.UsernamePasswordAuthenticationToken 
at org.springframework.util.Assert.isTrue(Assert.java:68) ... 

anglarjs驗證功能:

var authenticate = function(credentials, callback) { 

    var headers = credentials ? {authorization : "Basic " 
     + btoa(credentials.username + ":" + credentials.password) 
    } : {}; 

    console.log("header",headers); 

    $http.get('user', {headers : headers}).success(function(data) { 
     if (data.name) { 
     $rootScope.authenticated = true; 
     } else { 
     $rootScope.authenticated = false; 
     } 
     callback && callback(); 
    }).error(function() { 
     $rootScope.authenticated = false; 
     callback && callback(); 
    }); 

    } 

彈簧安置控制器:

@RestController 
public class RestApiController { 

@RequestMapping("/user") 
    public Principal user(Principal user) { 
    return user; 
    } 
... 

安全配置:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 
@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .inMemoryAuthentication() 
      .withUser("user").password("password").roles("USER"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
    .httpBasic() 
    .and() 
    .authorizeRequests() 
     .antMatchers("/index.jsp", "/resources/**","/").permitAll() 
     .anyRequest().authenticated().and() 
     .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class) 
     .csrf().csrfTokenRepository(csrfTokenRepository());; 
} 

private CsrfTokenRepository csrfTokenRepository() { 
     HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
     repository.setHeaderName("X-XSRF-TOKEN"); 
     return repository; 
    } 
} 

回答

1

春天使用設置HttpMessageConverts以適當的格式,例如,序列化/反序列化請求/響應。 JSON/HTML等您的實現看起來你沒有適當的消息轉換器,它可以序列化你的響應主體中的對象principal。爲了解決這個問題,你需要在你的類路徑中包含jackson-mapper-asl * .jar和jackson-databind * .jar

相關問題