0

我已经实现了一个演示客户端,它将用户名和密码作为 POST 发送到一个 REST Api,该 API 读取并执行一些身份验证逻辑。问题是在控制器中总是捕获为空(没有用户没有通过)。我尝试在 Chrome 的 Advanced Rest Client 中发送相同的数据,一切正常。

这是我的代码段:

角客户端:

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("/");
    });
};

};

控制器java(第一次尝试):

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

控制器java(第二次尝试):

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 传递到我的控制器的正确方法是什么?,我看不到错误...

感谢您的帮助!!!

4

1 回答 1

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>

用户资源.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));
    }

}
于 2013-09-16T08:42:12.767 回答