Spring 安全 4 和 JSF 2 集成

Spring Security 4 and JSF 2 integration

有没有办法集成 Spring Security 4(主要用于管理用户访问级别和他们可以访问哪些视图)和 JSF 2?

我发现 this neat thing 允许您将 Spring Boot 和 JSF 2 与 PrimeFaces 5 混合使用。很棒的东西。我想看看你能不能把它踢得更上一层楼

通常您会像这样为 Spring MVC 配置 Spring 安全性:

WebSecurityConfig.java

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()

                .and()

                .formLogin()
                .loginPage("/login")
                .permitAll()

                .and()

                .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("Zyst").password("password").roles("USER");
    }
}

据我所知,如果我弄错了,请纠正我,查看你的 MvcConfig 以了解“/home”等的实际含义:

MvcConfig.java

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }
}

但是,我已经在谷歌上搜索了几个小时,但无法真正找到关于如何为 JSF 配置 Spring 安全性的决定性答案。您能否使用 JSF 实现您的前端,然后使其由 Spring 安全管理,因此,例如链接,即:localhost:8080/home 而不是 localhost:8080/home.xhtml妥善管理和服务?并且 WebSecurityConfig.java 中定义的用户级别只能访问与其相关的页面。

从我(简要地)调查的情况来看,这可能是不可能的,因为 Faces 和 Mvc 是不同的技术,不能很好地协同工作。但是,如果可能的话,我想确定它是否可能。

如果可能的话,您能否提供一个工作示例,或者 link 更深入的地方?我做了 google 很多,但 100% 可能我最终遗漏了一些东西。

非常感谢任何和所有答案。

使用Spring Boot,Spring Security,JSF和Spring Core都没有问题,最后,JSF视图被解析为 urls,这就是您在 Spring 安全性中的工作。这是我自己的应用程序中的配置示例,我对其进行了一些删减以最大程度地减少代码量。代码不言自明:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

        // Have to disable it for POST methods:
        // 
        http.csrf().disable();

        // Logout and redirection:
        // 
        http.logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .invalidateHttpSession(true)
                .logoutSuccessUrl(
                        "/login.xhtml");

        http.authorizeRequests()
                // Some filters enabling url regex:
                // 
                .regexMatchers(
                        "\A/page1.xhtml\?param1=true\Z",
                        "\A/page2.xhtml.*")
                .permitAll()
                //Permit access for all to error and denied views
                .antMatchers("/500.xhtml", "/denied.xhtml")
                .permitAll()
                // Only access with admin role
                .antMatchers("/config/**")
                .hasRole("ADMIN")
                //Permit access only for some roles
                .antMatchers("/page3.xhtml")
                .hasAnyRole("ADMIN", "MANAGEMENT")
                //If user doesn't have permission, forward him to login page
                .and()
                .formLogin()
                .loginPage("/login.xhtml")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/main.xhtml")
                .and().exceptionHandling().accessDeniedPage("/denied.xhtml");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        //Configure roles and passwords as in-memory authentication
        auth.inMemoryAuthentication()
                .withUser("administrator")
                .password("pass")
                .roles("ADMIN");
        auth.inMemoryAuthentication()
                .withUser("manager")
                .password("pass")
                .roles("MANAGEMENT");
    }
}

当然,此代码适用于 *.xhtml 后缀 url,因为它们由 JSF Servlet 提供。如果你想避免这个后缀,你应该使用一个 url 重写工具作为 Prettyfaces。但这是另一个已经在 Whosebug 中广泛讨论的故事。

此外,请记住 将您的登录表单定位到配置的登录处理 url 让 Spring 安全处理身份验证和重定向到您的主页.我通常做的是使用非 JSF 表单并在其上应用 Primefaces 样式:

<form id="login_form" action="#{request.contextPath}/login" method="post">
    <p>
        <label for="j_username" class="login-form-tag">User</label> <input
            type="text" id="username" name="username" class="ui-corner-all"
            required="required" />
    </p>
    <p>
        <label for="j_password" class="login-form-tag">Password</label>
        <input type="password" id="password" name="password"
            class="ui-corner-all" required="required" />
    </p>
    <p>
        <button type="submit"
            class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
            <span class="ui-button-text">Login</span>
        </button>
    </p>
</form>

另请参阅: