Spring 启动: Secured RESTful API using Spring Social and Spring Security

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

我正在尝试使用 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 社交

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

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

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

  • 另一个Spring Boot tutorial for OAuth也不错并且有效,但如果可能的话,我想坚持使用 Spring Social,因为它很简单。

  • This post ,有助于解决使用上述 View 时 /connect/facebook 重定向的Method not allowed问题。

  • 发布关于 Social Config 。也许,我错过了一些东西。

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

谢谢!

更新1

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

最佳答案

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;
}

关于 Spring 启动: Secured RESTful API using Spring Social and Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786065/

相关文章:

java - 我不能使用 .properties "override"@NotEmpty 消息

java - org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties

java - 使用 JWT 和 Spring 时登录失败

java - Spring:获取特定接口(interface)和类型的所有 Bean

java - AWS 上的架构 : Running a distributed algorithm on dynamic nodes

java - JPA的批量更新触发TransactionalEventListener?

grails - 将 OpenAM、tomcat J2EE 策略代理与支持 Spring Security 的 Web 应用程序集成

java - Spring Security - 使用主体登录 OAuth/Google 后如何检索数据(如电子邮件、姓名、图片等)?

java - 如何覆盖 "No web page was found for the web address"消息?

spring-boot - Spring Boot 测试似乎正在创建与我预期不同的 H2 测试数据库