2014-12-03 3 views
0

Я хочу добавить безопасность в проект REST для весенних ботинок. Я использую собственную пользовательскую аутентификацию с контроллером и БД, а не базовым, а не OAuth.Как сделать службу REST с пружинным ботинком с безопасностью?

Есть ли хорошие учебные пособия о том, как добавить безопасность в проект REST для весенней загрузки?

Я использую конфигурацию безопасности:

package com.example.control.api; 

import com.example.control.api.shared.service.DatabaseAuthenticationProvider; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.HttpMethod; 
import org.springframework.security.authentication.AuthenticationProvider; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 
import org.springframework.security.web.context.HttpSessionSecurityContextRepository; 
import org.springframework.security.web.context.SecurityContextRepository; 


@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .requestMatchers() 
       .antMatchers("/", "/greeting") 
       .antMatchers(HttpMethod.POST, "/api/authentication/authenticate") 
       .and() 
       .authorizeRequests() 
       .anyRequest().authenticated(); 
    } 

    @Bean 
    AuthenticationProvider authenticationProvider() { 
     return new DatabaseAuthenticationProvider(); 
    }; 

    @Bean 
    SecurityContextRepository securityContextRepository(){ 
     return new HttpSessionSecurityContextRepository(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationProvider()); 
    } 
} 

Я использую этот контроллер для проверки подлинности:

package com.example.control.api.authentication; 

import com.example.control.api.shared.dao.IUserDAO; 
import com.example.control.api.shared.domain.Employee; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.security.authentication.AuthenticationManager; 
import org.springframework.security.authentication.BadCredentialsException; 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.security.web.context.SecurityContextRepository; 
import org.springframework.web.bind.annotation.*; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@RestController 
@RequestMapping("/api/authentication") 
public class AuthenticationController { 

    @Autowired 
    //@Qualifier("authenticationManager") 
    AuthenticationManager authenticationManager; 

    @Autowired 
    SecurityContextRepository repository; 

    @Autowired 
    IUserDAO userDAO; 

    @RequestMapping(value = "/authenticate", method = RequestMethod.POST) 
    @ResponseBody 
    public ResponseEntity login(
      @RequestBody AuthenticationDataModel authData, 
      HttpServletRequest request, HttpServletResponse response) { 
     UsernamePasswordAuthenticationToken token = 
       new UsernamePasswordAuthenticationToken(authData.getUsername(), authData.getPassword()); 
     try { 
      Authentication auth = authenticationManager.authenticate(token); 
      SecurityContextHolder.getContext().setAuthentication(auth); 
      repository.saveContext(SecurityContextHolder.getContext(), request, response); 
      return new ResponseEntity((Employee)auth.getDetails(), HttpStatus.OK); 
     } catch (BadCredentialsException ex) { 
      return new ResponseEntity<Employee>(HttpStatus.UNAUTHORIZED); 
     } 


    } 
} 

И я получил ошибку, когда запрос метода login этого контроллера:

java.lang.IllegalStateException: Cannot invoke saveContext on response [email protected]f4cfa. You must use the HttpRequestResponseHolder.response after invoking loadContext 
    at org.springframework.security.web.context.HttpSessionSecurityContextRepository.saveContext(HttpSessionSecurityContextRepository.java:111) 
    at com.example.control.api.authentication.AuthenticationController.login(AuthenticationController.java:46) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

ответ

0

Пожалуйста, перейдите по этой ссылке Spring Security Configuration in Spring Boot

Вы должны знать три вещи

  1. Добавить правила безопасности от вашего XML/Java/заводной конфигурации
  2. Отключить По умолчанию Spring безопасности в проекте Spring загрузки - Вам нужно создать или обновить файл с именем приложения .properties со следующей строкой: security.basic.enabled = ложная и поместить этот файл в SRC/главный/ресурсом
  3. в случае, если вы хотите использовать базовую безопасность, просто добавьте аннотацию к вашей Java конфигурации - @EnableWebSecurity
+0

Я использую Контроллер REST для аутентификации. Ваше решение не работает в моем случае. Я добавил больше информации о реализации. Надеюсь, ты поможешь мне. – Lunigorn

0

У меня было точно такое же сообщение об ошибке. В моем случае я был настолько сконцентрирован на конфигурации Java (я переключился с XML), что я забыл создать подкласс AbstractSecurityWebApplicationInitializer так:

public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer { 
} 

За дополнительной информацией вы можете обратиться к: http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/

Смежные вопросы