【问题标题】:Spring Boot: Secured RESTful API using Spring Social and Spring SecuritySpring Boot:使用 Spring Social 和 Spring Security 的安全 RESTful API
【发布时间】:2016-04-19 13:42:19
【问题描述】:

我正在尝试使用 Spring Boot 定义和保护 RESTful API。理想情况下,我想使用 Spring Social 并允许客户端(网络和移动)通过 Facebook 登录。

什么是有效的

到目前为止,我设法使用@RestController 获得了一个有效的 API,并使用如下基本 Spring Security 配置来保护它:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/api/**").authenticated()
                .antMatchers(HttpMethod.PUT, "/api/**").authenticated()
                .antMatchers(HttpMethod.DELETE, "/api/**").authenticated()
                .anyRequest().permitAll()
            .and().httpBasic()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
}

antMatchers 可能会得到改进,但我现在这样做是为了我自己的清晰度,它工作正常。允许执行 GET 请求,并且所有其他请求都需要发送 Spring Security 在运行时给出的标准user:password。使用httpie 的示例:

http POST user:a70fd629-1e29-475d-aa47-6861feb6900f@localhost:8080/api/ideas/ title="My first idea"

哪个正确的凭据,它会返回一个200 OK,否则返回一个401 Unauthorized

春季社交

现在,我被困住了,无法使用Spring-Social-Facebook 来使用我当前的设置并保持完全 RESTful 控制器。使用标准表单和重定向似乎微不足道,但我找不到任何解决方案来支持基于 REST 的方法,例如轻松支持 Web 和移动客户端。

据我了解,客户端必须处理流程,因为后端不会向/connect/facebook URL 发送任何重定向。

  • 我按照教程Accessing Facebook Data 进行操作,它可以自行运行。但是,我想避免像教程中那样使用facebookConnect.htmlfacebookConnected.html 模板。所以我不知道如何改变它。

  • 另一个Spring Boot tutorial for OAuth 也很好用,但由于简单,我想尽可能坚持使用Spring Social

  • This post,在使用上述视图时帮助解决了Method not allowed 重定向问题。

  • 发布关于Social Config的信息。可能,我在那里遗漏了一些东西。

任何建议、解决方案或指向更好教程的链接都会非常有帮助。

谢谢!

更新 1

现在,我有一个使用传统用户注册和登录表单的工作网站。我有一个“使用 Facebook 登录”按钮,它通过“OAuth 舞蹈”发送给我。所以下一个问题是我必须在 Facebook 登录成功后以某种方式手动创建用户,因为目前两个“登录”都不相关,所以即使用户使用 Facebook 登录,他还没有具有正确权限的关联用户对象。

【问题讨论】:

  • 嗨,拉尔夫,你能分享一下你最后是怎么做的吗.. 一个能够使用社交和普通表单注册为 Web 和移动应用程序注册/登录的休息 api .. 这正是我需要,但我遇到了和你一样的问题
  • 您有没有得到满意的解决方案?我有完全相同的要求,因为我希望通过 facebook 登录来保护我的 REST api,并且在我的 RESTful api 中没有带有视图的控制器(例如facebookConnect.html)的情况下,我无法理解 oauth dance 在这种情况下是如何工作的/跨度>
  • 有没有人得到了最终的解决方案,所有的信息都非常分散,我相信很多人在移动应用上实现这个问题时都会遇到这个问题
  • 我有same issue,以防你能提供答案。

标签: spring spring-security spring-boot spring-social spring-restcontroller


【解决方案1】:

SocialAuthenticationFilter 默认情况下重定向到'/signup' 在您描述的情况下,用户从社交应用程序登录,但是不存在本地帐户。您可以提供处理程序来创建本地帐户。这也包含在spring-socal 示例中。

@RequestMapping(value = { "/signup" }, method = RequestMethod.GET)
public String newRegistrationSocial(WebRequest request, Model model) throws Exception {
    String view = "redirect:/home";
    try {
        Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
        if (connection != null) {
            UserProfile up = connection.fetchUserProfile();

            registerUser(up.getFirstName(), up.getLastName(), up.getEmail(), "DummyPassword");              
            providerSignInUtils.doPostSignUp(up.getEmail(), request);

            //SignInUtils.signin(up.getEmail());
            ...
            ...             
        } 
    } 
    return view;
}

【讨论】:

  • 我没有得到 (see here) 是如何正确重定向 Web 客户端。我无法返回 redirect:/home 之类的内容,因为 Web 客户端与其他 api 是分开的。
猜你喜欢
  • 2017-07-07
  • 2014-03-01
  • 2015-05-04
  • 1970-01-01
  • 2019-06-25
  • 2016-02-12
  • 2012-06-30
  • 2013-04-01
  • 2015-07-04
相关资源
最近更新 更多