当前位置: 首页 > 知识库问答 >
问题:

Spring Security 4和JSF 2集成

贝礼骞
2023-03-14

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

我发现了这个巧妙的东西,它允许您将Spring Boot和JSF 2与PrimeFaces 5混合使用。很棒的东西。我想看看你能否把它提升到另一个层次。

通常您会为Spring MVC配置Spring Security,如下所示:

WebSecurity配置。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,看看“/家”等的实际含义:

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");
    }
}

然而,我在google上搜索了几个小时,并没有找到一个确切的答案,即如何为JSF配置Spring Security性。您是否可以使用JSF实现前端,然后让它由Spring Security管理,例如链接,即:localhost:8080/home而不是localhost:8080/home。xhtml是否得到了适当的管理和服务?这样用户级别就可以在WebSecurityConfig中定义。java只能访问与其自身相关的页面。

从我(简要地)调查的情况来看,这可能是不可能的,因为Faces和Mvc是不同的技术,不能很好地结合在一起。然而,如果可能的话,我想确定这是否可能。

如果可能的话,你能提供一个有效的例子,或者一个链接到更深入的地方吗?我在谷歌上搜索了不少,但很有可能我最终错过了什么。

非常感谢您的所有回答。

共有1个答案

吕子真
2023-03-14

将Spring Boot、Spring Security、JSF和Spring Core一起使用没有问题,最终,JSF视图被解析为URL,这就是您在Spring Security中使用的内容。这是我自己的应用程序中的配置示例,我对其进行了一些删减以最小化代码量。代码不言自明:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

        // Have to disable it for POST methods:
        // http://stackoverflow.com/a/20608149/1199132
        http.csrf().disable();

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

        http.authorizeRequests()
                // Some filters enabling url regex:
                // http://stackoverflow.com/a/8911284/1199132
                .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。但这是StackOverflow中已经广泛讨论的另一个故事。

此外,请记住将您的登录表单定位到配置的登录处理url,以让Spring Security处理到主页的身份验证和重定向。我通常会使用非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>

另请参见:

  • Spring和JSF集成
 类似资料:
  • 我用Eclipse创建了一个新项目 源文档:jndi:/localhost/jsf测试portlet/WEB-INF/faces-config。xml原因:Class'org。springframework。网状物jsf。埃尔。SpringBeanFacesELResolver'缺少运行时依赖项:java。lang.NoClassDefFoundError:org/springframework/

  • 我正在用PrimeFaces3.5、JasperReports5.2和JSF2.x开发一个应用程序。我的旧应用程序中有一些报告页面是由JasperReports4.5和JSF1.2编写的。所有这些报表页都在旧应用程序中工作。我试图在我的新应用程序中转移那些报告页。在新的应用程序中一切正常,但报告不能导出,我没有得到任何错误。我是不是漏掉了什么? 下面是我的代码,它在我的旧JSF1.2应用程序中工作

  • Glassfish 3.1.2,Mojarra 2.1.6,SSL已激活 也是一篇关于资源缓存的好文章。在我们的应用服务器中,SSL被激活。我们看到静态资源(图像、脚本、css)没有缓存。 下面是我的测试筛选器: 过期:确定。它是一个静态资源,不会改变,因此我们将到期日期设置在未来的一个月。 上次修改:不确定。我读到,将此设置为过去也会影响缓存 缓存控制:OK。允许安全缓存。安全影响? 此设置是否

  • 我使用Spring 3.2.2与Primeface 4.0和Hibernate 4.2。我在我的实体上有JSR303验证注释,并且我配置了Spring来在服务层验证它们——这很好。 但是我希望在调用服务之前启动JSF验证,但事实并非如此。我所做的所有研究都表明,我只需在类路径中添加一个验证程序,JSF2就会自动应用。 我已将建议的JAR添加到我的类路径中: 完整的依赖关系树可以在https://g

  • 你能提供一个链接到这些问题的解释(或者告诉我我做错了什么?)。我想我在谷歌搜索它失败了,因为互联网上充斥着关键词JSF和验证,对不起。

  • 问题内容: 假设我有一个管理用户的应用程序。您可以添加新用户,删除他们,编辑详细信息等。每个用户都有一个ID,并在URL上具有详细信息页面,如下所示: 现在,如果ID 123的用户不存在怎么办?我认为自然反应将是404标准错误。与您在URL中打错字(例如/user/dtail.jsf)时所输出的完全相同。所以问题是:有这种方法吗? 还是这个反应(404)合适? 谢谢。 问题答案: 只需将验证器附加

  • 我想,我的java代码是正确的,但记录并没有显示在dataTable上。 请检查下面的代码。我不知道我在哪里犯了错误。 .xhtml JAVA getResultList getResultList方法返回一个正确的值,我调试它。我认为问题在于userDataList。

  • 在JSF2应用程序中,当文件中没有明确提到会话超时时,会话超时是什么? 更新:我正在使用Tomcat,请参阅此处有关Tomcat中默认超时的相关帖子。