Spring Security 4和JSF 2集成

问题描述 投票:5回答:1

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

我发现this neat thing允许你将Spring Boot和JSF 2混合使用PrimeFaces 5.很棒的东西。我想看看你是否可以将其提升到另一个级别。

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

Web security config.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”之类的含义:

MVC config.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 Security。你可以使用JSF实现你的前端,然后由Spring Security管理,所以,例如Links,即:localhost:8080 / home而不是localhost:8080 / home.xhtml是否得到妥善管理和服务?因此,WebSecurityConfig.java中定义的用户级别只能访问与自己相关的页面。

从我(简要)调查的结果来看,由于Faces和Mvc是不同的技术并不是特别合适,所以可能无法实现。但是,如果可能的话,我想确定它是否可能。

如果可能的话,你能提供一个工作实例,或者提供更深入的地方的链接吗?我做了相当多的谷歌,但它是100%可能我最终错过了一些东西。

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

spring jsf spring-security spring-boot
1个回答
8
投票

使用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>

也可以看看:

© www.soinside.com 2019 - 2024. All rights reserved.