簡體   English   中英

如何制作angularjs和spring安全性登錄頁面?

[英]how to make angularjs and spring security login page?

這是我的登錄控制器:

app.controller('loginCtrl', function($scope,$http,$state,$location,$q) {

      var self = this;
        self.user={uname:'',password:''};
        self.users=[];
    this.postForm=function(user)
    {
         var deferred = $q.defer();
            $http.post('http://localhost:8080/HB2/login/', user)
                .then(
                function (response) {
                    deferred.resolve(response.data);
                    console.log("Success");
                },
                function(errResponse){
                    console.error('Invalid Username/Password');
                    deferred.reject(errResponse);
                }
            );
            return deferred.promise;
        };

這是我的其余登錄api:

@RequestMapping(value ="/login/", method = RequestMethod.POST,produces= MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> loginUser(@RequestBody loginModel user,    UriComponentsBuilder ucBuilder) {


        if ((user.getUname().equals("kartik"))&&(user.getPassword().equals("gogia"))) {

            return new ResponseEntity<Void>(HttpStatus.OK);
        }
        else
        return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
    }

幫助我如何登錄並轉移到主頁。

您可以將登錄表單用作Rest Url,如下所示:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginProcessingUrl("/app/authentication/oauth")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/app/logout")
                .deleteCookies("JSESSIONID")
                .permitAll()
                .and()
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/api/**").authenticated();
    }

現在,當您可以使用j_username和j_password從angular js控制器請求/ app / authentication / oauth時,它將自動轉到您的ApplicationDetailService中。

您可以嘗試以下代碼

    @RequestMapping(value="/login", method=RequestMethod.POST)
    public ModelAndView loginUser(@RequestBody loginModel user,  HttpServletRequest request, HttpServletResponse response){   
        String returnView = "home";//home.html

        try{    
            //try validating user..throw excception if user doesn't have access..                       
        }catch (InvalidUserException e){
            returnView = "error";//error.html
        }       
        ModelAndView mv = new ModelAndView(returnView);
        return mv;
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM