首页 文章

Spring Security OAuth2 - 使用服务器检查可选登录

提问于
浏览
0

我正在使用不同的Spring Boot WebMVC客户端开发Web项目 . 其中一些客户端需要授权,我使用Spring Security OAuth2服务器解决了这个问题 . 身份验证工作正常,我没有问题 . 有些客户不需要登录,他们是公开的 .

Technical facts :所有客户端都使用Angular,jQuery和简单JSP之间的混合 . 所有应用都使用Spring Security,公共应用配置如下:

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http.antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/**").permitAll()
            .antMatchers("/fonts/**").permitAll()
            .anyRequest().authenticated();
}

Now my question :我计划在所有应用的标头中构建一个登录(或注销)按钮 . 在具有所需身份验证的应用程序中是没有问题的 . 我可以检查校长是否为空 . 但是我如何在公共应用中解决这个问题 . 校长永远无效,客户也没有工作 .

也许它会有所帮助 - 我的两个应用程序:

UPDATE: Maybe a better example

如果我配置这样的公共应用程序

@Configuration
@EnableOAuth2Sso
public static class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated();
    }
}

和MVC控制器一样

@RequestMapping("/{([a-z]{2})}")
public ModelAndView start(final Principal principal) {
    return new ModelAndView("start");
}

那么校长永远是空的 . 我认为这是我的问题 . 我需要检查OAuth服务器,如果我登录是主要集,如果我没有登录,它应该为null .

1 回答

  • 0

    如果我正确理解了您的问题,那么您可以在不进行身份验证的情况下访问某些URL模式 . 在这种情况下,您可以使用以下方法来阻止对某些URL模式的身份验证 -

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

    permitAll() 方法定义所有 authenticated 用户都可以访问提到的URL模式 . 因此,如果您希望某些用户在没有身份验证的情况下访问某些资源(URL),那么您必须使用上述方法 .

相关问题