Spring安全只允许oauth登录

时间:2017-04-25 09:57:29

标签: spring oauth

在我的项目中,我只想允许oauth登录。我的春季安全配置如下:

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/login.html", "/oauth/**", "/sign**").permitAll()
        .anyRequest().authenticated()
        .and()
            .formLogin().disable()
            .httpBasic().disable()[enter image description here][1]
            .exceptionHandling().accessDeniedPage("/login.html")
        .and()
            .apply(new SpringSocialConfigurer());
}

问题是访问被拒绝的页面无效。我想获得login.html内容如下:
login.html

实际上我得到403页如下:
403

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,代码如下:

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login.html", "/oauth/**", "/sign**").permitAll()
        .anyRequest()
            .authenticated()
        .and()
            .exceptionHandling()
            .defaultAuthenticationEntryPointFor(new HttpForbiddenEntryPoint(), AnyRequestMatcher.INSTANCE)
        .and()
            .logout()
            .logoutUrl("/logout.html")
            .clearAuthentication(true)
            .logoutSuccessUrl("/login.html")
        .and()
            .apply(new SpringSocialConfigurer());

}

切入点:

public class HttpForbiddenEntryPoint implements AuthenticationEntryPoint {

    private String redirect_url = "/login.html";

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        response.sendRedirect(redirect_url);

    }

    public String getRedirect_url() {
        return redirect_url;
    }

    public void setRedirect_url(String redirect_url) {
        this.redirect_url = redirect_url;
    }
}
相关问题