我已经在angular中实现了一个演示客户端,它将用户名和密码作为POST发送到REST Api,然后读取它并执行一些auth逻辑。问题是在控制器中始终捕获为null(没有用户没有通过)。我尝试在Chrome中的Advanced Rest Client中发送相同的数据,一切正常。
这是我的代码段:
Angular客户:
function LoginCtrl($scope, $rootScope, $location, $http, LoginService) {
$scope.submit = function() {
LoginService.authenticate($.param({username: $scope.inputUsername, password: $scope.inputPassword}), function(user) {
$rootScope.user = user;
$http.defaults.headers.common['Authentication'] = user.token;
$location.path("/");
});
};
};
Controller java(First Try):
public @ResponseBody UserTransfer authenticate(HttpServletRequest request) throws IOException {
//tried this one first but NULL this two params.
String username = request.getParam("username");
String password = request.getParam("password");
//somer logic
return new UserTransfer(userDetails.getUsername(), roles, TokenUtils.createToken(userDetails));
Controller java(Second Try):
public @ResponseBody UserTransfer authenticate(@RequestParam("username") String username, @RequestParam("password") String password) throws IOException {
//tried secondly this one but in this case the server responded with ERROR 400 - Bad Request
//somer logic
return new UserTransfer(userDetails.getUsername(), roles, TokenUtils.createToken(userDetails));
将这些数据从Angular传递给我的控制器的正确方法是什么?我看不出错误......
感谢您的帮助!!!
答案 0 :(得分:0)
有同样的问题 用泽西来处理那个
的web.xml
<servlet>
<servlet-name>RestService</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.pearson.taxonomy.cm.web.controller</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
UserResource.java
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;
@Component
@Path("/user")
public class UserResource {
@Autowired
private UserDetailsService userService;
@Autowired
@Qualifier("authenticationManager")
private AuthenticationManager authManager;
@Path("authenticate")
@POST
@Produces(MediaType.APPLICATION_JSON)
public UserView authenticate(@FormParam("username") String username, @FormParam("password") String password) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
Authentication authentication = this.authManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
Map<String, Boolean> roles = new HashMap<String, Boolean>();
UserDetails userDetails = this.userService.loadUserByUsername(username);
for (GrantedAuthority authority : userDetails.getAuthorities()) {
roles.put(authority.toString(), Boolean.TRUE);
}
return new UserView(userDetails.getUsername(), roles, TokenUtils.createToken(userDetails));
}
}