2014-11-25 99 views
1

我想要的鑑別添加到我的RestController,但我不能找到任何好的文件或與Java配置任何樣品。Spring安全方法級別安全與Java配置+ REST身份驗證?

我試過,但它不工作(我可以訪問,而無需登錄的所有請求)

我的控制器都被註解@PreAuthorize

@RestController 
@RequestMapping("/api/hello") 
public class HelloController { 

    @RequestMapping(value = "/say", method = RequestMethod.GET) 
    public String sayHello() { 
     return "hello"; 
    } 

    @PreAuthorize("hasRole('ROLE_USER')") 
    @RequestMapping(value = "/say/user", method = RequestMethod.GET) 
    public String sayHelloWithUserProtection(){ 
     return "Hello USER"; 
    } 

    @PreAuthorize("hasRole('ROLE_ADMIN')") 
    @RequestMapping(value = "/say/admin", method = RequestMethod.GET) 
    public String sayHelloWithAdminrProtection(){ 
     return "Hello ADMIN"; 
    } 
} 

SecurityConfig

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@ComponentScan(basePackages = {"com.test.server.security"}) 
public class SecurityConfig { 


    @Autowired 
    public void configureAuthentification(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
     .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER").and() 
       .withUser("admin").password("admin").roles("USER","ADMIN"); 
    } 

    @Configuration 
    public static class ApiWebConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .antMatcher("/api/**") 
        .formLogin(); 
     } 
    } 

} 

SecurityWebApplicationInitializer

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 
} 

我該如何讓它工作?

而有任何好的教程,使基於REST令牌(令牌,該令牌保存會話密鑰和其他自定義值)與JPA(或JDBC?)與Java的配置保存在數據庫認證?

回答

0

我忘了把我的WebApplicationInitialisation的問題。

我的錯誤是我把SecurityConfig在getRootConfigClasses()代替getServletConfigClasses的()。 現在WebApplicationInitialisation看起來像這樣,它工作得很好!

public class WebApplicationInitialisation extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[]{RootConfig.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[]{WebMvcConfig.class, SecurityConfig.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[]{"/"}; 
    } 


    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     super.onStartup(servletContext); 
    } 
} 
+0

我不明白,你能詳細解釋一下嗎?,我從web.xml開始我的Spring配置 – pazfernando 2015-11-27 00:08:19

2

降formLogin()。你需要保持REST應該是無狀態的思維模式。用這種方式登錄表單並不是純粹的REST。你可以用這樣的Spring安全鏈創建一個精美的蒙面過濾器(隨機添加東西來創建一個更完整的過濾器,這意味着你需要在創建過濾器之前創建一個實際的過濾器。你需要他們匹配的路徑之前,授權請求。

http.authorizeRequests() 
     .antMatchers("/login").permitAll() 
     .antMatchers("/say/user/").hasRole("USER") 
     .antMatchers("/say/admin").hasRole("ADMIN") 
     .anyRequest().authenticated(); 

上面的代碼應該自我解釋。如果沒有,我會盡量詳細說明就可以了。

至於基於令牌登錄,這是一個好主意,但你不應該推出自己的。 春天有很好的Oauth支持和g Etting開始保護你的REST API,它非常棒。

本教程將詳細解釋它,並應該幫助您進一步構建更好的API。 http://spring.io/guides/tutorials/bookmarks/

另外,還要確保你看看福勒的著作就在這裏休息 http://martinfowler.com/articles/richardsonMaturityModel.html

+0

感謝您的回答,我看着OAuth,我會讓你知道的。 我發現本教程http://jaxenter.com/rest-api-spring-java-8-112289.html。 但是沒有辦法使用@PreAuthorize而不是antMatchers? (我認爲它更容易維護) – alvinmeimoun 2014-11-25 12:44:25

+0

您可以同時使用,但我不會忽略antMatchers。 – scav 2014-12-02 13:44:53