2015-05-21 7 views
36

मैं राज्यविहीन वसंत सुरक्षा के लिए उपयोग कर रहा हूँ वसंत सुरक्षा को अक्षम करने के लिए, लेकिन साइनअप के मामले में मैं वसंतकैसे विशेष यूआरएल

antMatchers("/api/v1/signup").permitAll(). 

का उपयोग कर security.I अक्षम अक्षम करना चाहते हैं, लेकिन यह काम नहीं कर रहा, मैं त्रुटि हो रही है नीचे:

message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException 

मैं इसका मतलब यह है वसंत सुरक्षा फिल्टर

काम कर रहे हैं मेरे यूआरएल के आदेश हमेशा "/ API/v1"

हो जाएगा लगता है 0

मेरे वसंत config

@Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http. 
     csrf().disable(). 
     sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS). 
     and(). 
     authorizeRequests(). 
     antMatchers("/api/v1/signup").permitAll(). 
     anyRequest().authenticated(). 
     and(). 
     anonymous().disable(); 
     http.addFilterBefore(new AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class); 
    } 

मेरे प्रमाणीकरण फिल्टर

@Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest httpRequest = asHttp(request); 
     HttpServletResponse httpResponse = asHttp(response); 

     String username = httpRequest.getHeader("X-Auth-Username"); 
     String password = httpRequest.getHeader("X-Auth-Password"); 
     String token = httpRequest.getHeader("X-Auth-Token"); 

     String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest); 

     try { 

      if (postToAuthenticate(httpRequest, resourcePath)) {    
       processUsernamePasswordAuthentication(httpResponse, username, password); 
       return; 
      } 

      if(token != null){ 
       processTokenAuthentication(token); 
      } 
      chain.doFilter(request, response); 
     } catch (InternalAuthenticationServiceException internalAuthenticationServiceException) { 
      SecurityContextHolder.clearContext(); 
      logger.error("Internal authentication service exception", internalAuthenticationServiceException); 
      httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
     } catch (AuthenticationException authenticationException) { 
      SecurityContextHolder.clearContext(); 
      httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage()); 
     } finally { 
     } 
    } 

    private HttpServletRequest asHttp(ServletRequest request) { 
      return (HttpServletRequest) request; 
     } 

     private HttpServletResponse asHttp(ServletResponse response) { 
      return (HttpServletResponse) response; 
     } 

     private boolean postToAuthenticate(HttpServletRequest httpRequest, String resourcePath) { 
      return Constant.AUTHENTICATE_URL.equalsIgnoreCase(resourcePath) && httpRequest.getMethod().equals("POST"); 
     } 

     private void processUsernamePasswordAuthentication(HttpServletResponse httpResponse,String username, String password) throws IOException { 
      Authentication resultOfAuthentication = tryToAuthenticateWithUsernameAndPassword(username, password); 
      SecurityContextHolder.getContext().setAuthentication(resultOfAuthentication); 
      httpResponse.setStatus(HttpServletResponse.SC_OK); 
      httpResponse.addHeader("Content-Type", "application/json"); 
      httpResponse.addHeader("X-Auth-Token", resultOfAuthentication.getDetails().toString()); 
     } 

     private Authentication tryToAuthenticateWithUsernameAndPassword(String username,String password) { 
      UsernamePasswordAuthenticationToken requestAuthentication = new UsernamePasswordAuthenticationToken(username, password); 
      return tryToAuthenticate(requestAuthentication); 
     } 

     private void processTokenAuthentication(String token) { 
      Authentication resultOfAuthentication = tryToAuthenticateWithToken(token); 
      SecurityContextHolder.getContext().setAuthentication(resultOfAuthentication); 
     } 

     private Authentication tryToAuthenticateWithToken(String token) { 
      PreAuthenticatedAuthenticationToken requestAuthentication = new PreAuthenticatedAuthenticationToken(token, null); 
      return tryToAuthenticate(requestAuthentication); 
     } 

     private Authentication tryToAuthenticate(Authentication requestAuthentication) { 
      Authentication responseAuthentication = authenticationManager.authenticate(requestAuthentication); 
      if (responseAuthentication == null || !responseAuthentication.isAuthenticated()) { 
       throw new InternalAuthenticationServiceException("Unable to authenticate Domain User for provided credentials"); 
      } 
      logger.debug("User successfully authenticated"); 
      return responseAuthentication; 
     } 

मेरे नियंत्रक है

@RestController 
public class UserController { 

    @Autowired 
    UserService userService; 

    /** 
    * to pass user info to service 
    */ 
    @RequestMapping(value = "api/v1/signup",method = RequestMethod.POST) 
    public String saveUser(@RequestBody User user) { 
     userService.saveUser(user); 
     return "User registerted successfully"; 
    } 
} 

मैं पूरी तरह वसंत के नया हूँ, मेरी मदद कैसे यह करने के लिए कृपया है?

+0

यहां देखें: http://stackoverflow.com/questions/4487249/how-do-i-ge टी-permitall-in-spring-security-to-not-throw-authenticationcredentials –

उत्तर

61

जब permitAll का उपयोग कर इसे हर प्रमाणीकृत उपयोगकर्ता का मतलब है, फिर भी आप विकलांग अनाम पहुंच ताकि काम नहीं करेगा।

configure विधि को ओवरराइड करने के लिए कुछ यूआरएल को अनदेखा करना है जो WebSecurity ऑब्जेक्ट और ignore पैटर्न लेता है।

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/api/v1/signup"); 
} 

और HttpSecurity भाग से उस पंक्ति को हटा दें। यह स्प्रिंग सिक्योरिटी को इस यूआरएल को अनदेखा करने के लिए बताएगा और उन्हें कोई फ़िल्टर लागू नहीं करेगा।

http 
    .authorizeRequests() 
    .antMatchers("/api/v1/signup/**").permitAll() 
    .anyRequest().authenticated() 
+3

यह किस फ़ाइल में लिखा जाता है? –

+1

@JacobZimmerman https://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/ वेब सुरक्षा वर्ग –

+0

के लिए कॉन्फ़िगरर मुझे एहसास है कि यह एक पुरानी पोस्ट है। केवल धन्यवाद कहना था।आपका समाधान तब विफल हुआ जब अन्य विफल रहे। मैंने इस तरह के कुछ भी उपयोग करने की कोशिश की: http । प्राधिकरण अनुरोध() .antMatchers ("/ api/v1/signup/**") परमिट सभी() .anyRequest() प्रमाणीकृत() – Carl

6
<http pattern="/resources/**" security="none"/> 

या जावा विन्यास के साथ:

web.ignoring().antMatchers("/resources/**"); 
वर्ष के बजाय

: exp के लिए

<intercept-url pattern="/resources/**" filters="none"/> 

। एक प्रवेश पृष्ठ के लिए सुरक्षा को अक्षम:

<intercept-url pattern="/login*" filters="none" /> 
5

मैं एक बेहतर तरीका हो मैंने पूर्ण कॉन्फ़िगरेशन शामिल किया है लेकिन मुख्य पंक्ति है:

.csrf().ignoringAntMatchers("/contact-email") 
5

यह आपके सवाल का पूरा जवाब नहीं हो सकता है आप CSRF सुरक्षा को निष्क्रिय करने के जिस तरह से आप कर सकते हैं के लिए देख रहे हैं लेकिन यदि:

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("/web/admin/**").hasAnyRole(ADMIN.toString(), GUEST.toString()) 
       .anyRequest().permitAll() 
       .and() 
       .formLogin().loginPage("/web/login").permitAll() 
       .and() 
       .csrf().ignoringAntMatchers("/contact-email") 
       .and() 
       .logout().logoutUrl("/web/logout").logoutSuccessUrl("/web/").permitAll(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
       .withUser("admin").password("admin").roles(ADMIN.toString()) 
       .and() 
       .withUser("guest").password("guest").roles(GUEST.toString()); 
    } 

} 

संबंधित मुद्दे