相关疑难解决方法(0)

由于cookie冲突(授权代码机制),官方Spring安全性oauth2示例不起作用

根据教程Spring Boot和OAuth2

我有以下项目结构:

在此输入图像描述

以下源代码:

SocialApplication.class:

@SpringBootApplication
@RestController
@EnableOAuth2Client
@EnableAuthorizationServer
@Order(200)
public class SocialApplication extends WebSecurityConfigurerAdapter {

    @Autowired
    OAuth2ClientContext oauth2ClientContext;

    @RequestMapping({ "/user", "/me" })
    public Map<String, String> user(Principal principal) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("name", principal.getName());
        return map;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
                .authenticated().and().exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
                .logoutSuccessUrl("/").permitAll().and().csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
        // @formatter:on
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception …
Run Code Online (Sandbox Code Playgroud)

java spring-security oauth-2.0 spring-oauth2

29
推荐指数
3
解决办法
3763
查看次数

Spring Boot:使用Spring Social和Spring Security保护RESTful API

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

什么工作

到目前为止,我设法使用一个有效的API @RestController并使用基本的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

http POST user:a70fd629-1e29-475d-aa47-6861feb6900f@localhost:8080/api/ideas/ title="My first idea"
Run Code Online (Sandbox Code Playgroud)

哪个权利凭据,它发200 OK回一个,否则一个401 Unauthorized.

春天社交

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

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

  • 我按照教程访问Facebook数据,它自己工作.但是,我想避免必须拥有教程中的那些facebookConnect.htmlfacebookConnected.html模板.所以我不知道如何改变它.

  • OAuth的另一个 …

spring spring-security spring-social spring-boot spring-restcontroller

9
推荐指数
1
解决办法
2541
查看次数