如何在spring security OAuth2中使用permitAll()在公共页面上共享用户主体/securityContext

如何在spring security OAuth2中使用permitAll()在公共页面上共享用户主体/securityContext,spring,spring-security,oauth-2.0,spring-session,Spring,Spring Security,Oauth 2.0,Spring Session,我们使用OAuth[LDAP/Social Login]在spring boot中开发了一个SSO应用程序。应用程序有一些公共页和一些受保护页。要求是,我们希望在公共页面上的一些内容基于经过身份验证的用户。问题是,一旦我们登录到一个应用程序并访问另一个应用程序的公共页面,用户主体在公共页面上就不可用,除非我们访问同一应用程序的其中一个受保护页面。任何实现这一点的建议/示例都将在此处有所帮助。下面是我的资源服务器代码 public class SocialApplication extends W

我们使用OAuth[LDAP/Social Login]在spring boot中开发了一个SSO应用程序。应用程序有一些公共页和一些受保护页。要求是,我们希望在公共页面上的一些内容基于经过身份验证的用户。问题是,一旦我们登录到一个应用程序并访问另一个应用程序的公共页面,用户主体在公共页面上就不可用,除非我们访问同一应用程序的其中一个受保护页面。任何实现这一点的建议/示例都将在此处有所帮助。下面是我的资源服务器代码

public class SocialApplication extends WebSecurityConfigurerAdapter {
@Autowired
CustomLdapAuthoritiesPopulator ldapAuthoritiesPopulator;

@Autowired
CustomLogoutSuccessHandler customLogoutSuccessHandler;

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

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/page1Prv");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.antMatcher("/**").authorizeRequests().antMatchers("/login", "/index**", "/webjars/**").permitAll()
            .anyRequest().authenticated().and().authorizeRequests().anyRequest().fullyAuthenticated().and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/").and().logout()
            .logoutSuccessHandler(customLogoutSuccessHandler).permitAll().and().csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
            .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
            //.userDetailsService(userDetailsService);

    http.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf().disable();
}

@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.ldapAuthentication()
    .contextSource()
            .url("ldap://localhost:10389/dc=example,dc=com").managerDn("uid=admin,ou=system")
            .managerPassword("secret").and()
            .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
            .userSearchBase("ou=users").userSearchFilter("(cn={0})");
}


@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/me").authorizeRequests().anyRequest().authenticated();
    }
}

public static void main(String[] args) {
    SpringApplication.run(SocialApplication.class, args);
}
公共类社交应用程序扩展了WebSecurity配置适配器{
@自动连线
CustomLdapAuthoritiesPopulator ldapAuthoritiesPopulator;
@自动连线
CustomLogoutSuccessHandler CustomLogoutSuccessHandler;
@请求映射({“/user”,“/me”})
公共地图用户(主体){
Map Map=newlinkedhashmap();
put(“name”,principal.getName());
返回图;
}
@凌驾
public void configure(WebSecurity web)引发异常{
忽略().antMatchers(“/page1Prv”);
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.antMatcher(“/**”).authorizeRequests().antMatchers(“/login”、“/index**”、“/webjars/**”).permitAll()
.anyRequest().authenticated()和().authorizeRequests().anyRequest().fullyAuthenticated()和()
.formLogin().loginPage(“/login”).defaultSuccessUrl(“/”)和()
.logoutSuccessHandler(customLogoutSuccessHandler).permitAll()和()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())和()
.addFilterBefore(ssoFilter(),BasicAuthenticationFilter.class);
//.userDetailsService(userDetailsService);
http.httpBasic()和().authorizeRequests().anyRequest().authorized()和().csrf().disable();
}
@凌驾
受保护的无效配置(AuthenticationManagerBuilder AuthenticationManagerBuilder)引发异常{
authenticationManagerBuilder.ldapaauthentication()
.contextSource()
.url(“ldap://localhost:10389/dc=example,dc=com”).managerDn(“uid=admin,ou=system”)
.managerPassword(“机密”)。和()
.ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
.userSearchBase(“ou=users”).userSearchFilter(“cn={0}”);
}
@配置
@EnableResourceServer
受保护的静态类ResourceServerConfiguration扩展了ResourceServerConfigurerAdapter{
@凌驾
public void configure(HttpSecurity http)引发异常{
http.antMatcher(“/me”).authorizeRequests().anyRequest().authorized();
}
}
公共静态void main(字符串[]args){
run(SocialApplication.class,args);
}

}

我从user()返回了主体对象。请参阅下面的代码,它解决了我的问题。
@RequestMapping({“/user”,“/me”})
公共主要用户(主要用户){
返还本金;
}