您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Spring Security 4和JSF 2集成

Spring Security 4和JSF 2集成

,最后,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中已经广泛讨论的另一个故事。

另外,请记住将 以让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>

Java 2022/1/1 18:15:40 有916人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶