2011-02-06 6 views
12

내 보안 프로젝트를 구현했습니다 : Spring Security 3 - MVC Integration Tutorial (Part 2).스프링 보안 Ajax 로그인

제 문제는 이것을 Ajax 기반 로그인으로 바꾸어야한다는 것입니다. 문자열/JSON을 클라이언트에 반환하는 것과이 XML을 적합하게 만들려면 어떻게해야합니까? form-login 태그에 문제가 있음을 이해합니다.

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <!-- This is where we configure Spring-Security --> 
    <http auto-config="true" use-expressions="true" access-denied-page="/Management/auth/denied" > 

     <intercept-url pattern="/Management/auth/login" access="permitAll"/> 
     <intercept-url pattern="/Management/main/admin" access="hasRole('ROLE_ADMIN')"/> 
     <intercept-url pattern="/Management/main/common" access="hasRole('ROLE_USER')"/> 

     <form-login 
       login-page="/Management/auth/login" 
       authentication-failure-url="/Management/auth/login?error=true" 
       default-target-url="/Management/main/common"/> 

     <logout 
       invalidate-session="true" 
       logout-success-url="/Management/auth/login" 
       logout-url="/Management/auth/logout"/> 

    </http> 

    <!-- Declare an authentication-manager to use a custom userDetailsService --> 
    <authentication-manager> 
      <authentication-provider user-service-ref="customUserDetailsService"> 
        <password-encoder ref="passwordEncoder"/> 
      </authentication-provider> 
    </authentication-manager> 

    <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database --> 
    <beans:bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/> 

    <!-- A custom service where Spring will retrieve users and their corresponding access levels --> 
    <beans:bean id="customUserDetailsService" class="com.affiliates.service.CustomUserDetailsService"/> 

</beans:beans> 

답변

19

이것은 오래된 게시물이지만 "봄 보안 ajax 로그인"의 결과 중 하나로 나타나므로 내 솔루션을 공유 할 것이라고 생각했습니다. 이는 Spring Security 표준을 따르며 설정이 매우 간단합니다. 보안 구성에 REST/Ajax와 나머지 앱 (일반 HTML 페이지)에 대해 하나씩 2 <http> 요소를 갖는 것이 트릭입니다. <http>이 나타나는 순서는 중요하며 더 구체적인 URL에서 <http> 안에있는 <url-intercept> 개의 요소와 같은 방식으로 이루어져야합니다.

1 단계 : 설치 두 별도 <http>

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <!-- a shared request cache is required for multiple http elements --> 
    <beans:bean id="requestCache" class="org.springframework.security.web.savedrequest.HttpSessionRequestCache" /> 

    <!-- remove security from static resources to avoid going through the security filter chain --> 
    <http pattern="/resources/**" security="none" /> 

    <!-- http config for REST services (AJAX interface) 
    =================================================== --> 
    <http auto-config="true" use-expressions="true" pattern="/rest/**"> 
     <!-- login configuration 
      login-processing-url="/rest/security/login-processing" front-end AJAX requests for authentication POST to this URL 
      login-page="/rest/security/login-page" means "authentication is required" 
      authentication-failure-url="/rest/security/authentication-failure" means "authentication failed, bad credentials or other security exception" 
      default-target-url="/rest/security/default-target" front-end AJAX requests are redirected here after success authentication 
     --> 
     <form-login 
      login-processing-url="/rest/security/login-processing" 
      login-page="/rest/security/login-page" 
      authentication-failure-url="/rest/security/authentication-failure" 
      default-target-url="/rest/security/default-target" 
      always-use-default-target="true" /> 
     <logout logout-url="/rest/security/logout-url" /> 

     <!-- REST services can be secured here, will respond with JSON instead of HTML --> 
     <intercept-url pattern="/rest/calendar/**" access="hasRole('ROLE_USER')" /> 

     <!-- other REST intercept-urls go here --> 

     <!-- end it with a catch all --> 
     <intercept-url pattern="/rest/**" access="isAuthenticated()" /> 

     <!-- reference to the shared request cache --> 
     <request-cache ref="requestCache"/> 
    </http> 

    <!-- http config for regular HTML pages 
    =================================================== --> 
    <http auto-config="true" use-expressions="true"> 
     <form-login 
      login-processing-url="/security/j_spring_security_check" 
      login-page="/login" 
      authentication-failure-url="/login?login_error=t" /> 
     <logout logout-url="/security/j_spring_security_logout" /> 

     <intercept-url pattern="/calendar/**" access="hasRole('ROLE_USER')" /> 

     <!-- other intercept-urls go here --> 

     <!-- in my app's case, the HTML config ends with permitting all users and requiring HTTPS 
      it is always a good idea to send sensitive information like passwords over HTTPS --> 
     <intercept-url pattern="/**" access="permitAll" requires-channel="https" /> 

     <!-- reference to the shared request cache --> 
     <request-cache ref="requestCache"/> 
    </http> 

    <!-- authentication manager and other configuration go below --> 
</beans:beans> 

2 단계 : REST 인증 제어기

import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import flexjson.JSONSerializer; 

@Controller 
@RequestMapping(value = "/rest/security") 
public class RestAuthenticationController { 

    public HttpHeaders getJsonHeaders() { 
     HttpHeaders headers = new HttpHeaders(); 
     headers.add("Content-Type", "application/json"); 
     return headers; 
    } 

    @RequestMapping(value="/login-page", method = RequestMethod.GET) 
    public ResponseEntity<String> apiLoginPage() { 
     return new ResponseEntity<String>(getJsonHeaders(), HttpStatus.UNAUTHORIZED); 
    } 

    @RequestMapping(value="/authentication-failure", method = RequestMethod.GET) 
    public ResponseEntity<String> apiAuthenticationFailure() { 
     // return HttpStatus.OK to let your front-end know the request completed (no 401, it will cause you to go back to login again, loops, not good) 
     // include some message code to indicate unsuccessful login 
     return new ResponseEntity<String>("{\"success\" : false, \"message\" : \"authentication-failure\"}", getJsonHeaders(), HttpStatus.OK); 
    } 

    @RequestMapping(value="/default-target", method = RequestMethod.GET) 
    public ResponseEntity<String> apiDefaultTarget() { 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     // exclude/include whatever fields you need 
     String userJson = new JSONSerializer().exclude("*.class", "*.password").serialize(authentication); 
     return new ResponseEntity<String>(userJson, getJsonHeaders(), HttpStatus.OK); 
    } 
} 

3 단계 : AJAX 양식을 제출하고 응답을 처리 할 필요가 jQuery의 ajaxForm 라이브러리

<form action="/rest/security/login-processing" method="POST"> 
... 
</form> 

$('form').ajaxForm({ 
    success: function(response, statusText, xhr, $form) { 
     console.log(response); 
     if(response == null || response.username == null) { 
      alert("authentication failure"); 
     } else { 
      // response is JSON version of the Spring's Authentication 
      alert("authentication success"); 
     } 
    }, 
    error: function(response, statusText, error, $form) { 
     if(response != null && response.message == "authentication-failure") { 
      alert("authentication failure"); 
     } 
    } 
}); 
+0

귀하의 솔루션은 좋은 것 같지만 곳 '에/휴식/보안/로그인 처리를 "정의 –

+0

@MichaelDeKeyser 응답 컨트롤러 방법 -이 SpringSecurity에서 제공 UsernamePasswordAuthenticationFilter 매핑 것, 워드 프로세서 및 검색에서보세요 'UsernamePasswordAuthenticationFilter'및 'login-processing-url'에 대해 http://docs.spring.io/autorepo/docs/spring-security/3.2.0.RELEASE/reference/htmlsingle/ – SergeyB

+1

Spring의 JavaConfig를 통해이 작업을 시도했습니다. 보안, 마침내 XML ftw! – Boon

2

스프링은 XML 기반 구성에서 Java @Configuration 클래스로 이동합니다. 아래는 위의 게시물 (Spring Security Ajax login)에서 설명한 설정의 @Configuration 버전입니다. 단계 2 & 3이 동일하게 유지되면 1 단계를이 코드로 바꿉니다. 순서가 다시 중요하며 더 일반적인 정의를로드하기 전에보다 구체적인 정의를로드해야하며이를 제어하려면 @Order(1)@Order(2)을 사용하십시오.

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.core.annotation.Order; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; 
import org.springframework.security.web.savedrequest.RequestCache; 

@Configuration 
@EnableWebSecurity 
public class WebMvcSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Bean(name = "requestCache") 
    public RequestCache getRequestCache() { 
     return new HttpSessionRequestCache(); 
    } 

    @Configuration 
    @Order(1) 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

      @Autowired private RequestCache requestCache; 

      @Override 
      protected void configure(HttpSecurity http) throws Exception { 
       http 
        .regexMatcher("/rest.*") 
        .authorizeRequests() 
         .antMatchers("/rest/calendar/**") 
          .hasAuthority("ROLE_USER") 
         .antMatchers("/rest/**") 
          .permitAll() 
         .and() 
        .headers() 
         .xssProtection() 
         .and() 
        .logout() 
         .logoutUrl("/rest/security/logout-url") 
         .and() 
        .requestCache() 
         .requestCache(requestCache) 
         .and() 
        .formLogin() 
         .loginProcessingUrl("/rest/security/login-processing") 
         .loginPage("/rest/security/login-page") 
         .failureUrl("/rest/security/authentication-failure") 
         .defaultSuccessUrl("/rest/security/default-target", false) 
         .and() 
        .httpBasic(); 
      } 
    } 

    @Configuration 
    @Order(2) 
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Autowired private RequestCache requestCache; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .authorizeRequests() 
        .regexMatchers("/calendar/.*") 
         .hasAuthority("ROLE_USER") 
        .regexMatchers("/.*") 
         .permitAll() 
        .and() 
       .logout() 
        .logoutUrl("/security/j_spring_security_logout") 
        .and() 
       .requestCache() 
        .requestCache(requestCache) 
        .and() 
       .formLogin() 
        .loginProcessingUrl("/security/j_spring_security_check") 
        .loginPage("/login") 
        .failureUrl("/login?login_error=t") 
        .and() 
       .httpBasic(); 
     } 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
      .ignoring() 
       .antMatchers("/resources/**") 
       .antMatchers("/sitemap.xml"); 
    } 
}