gpt4 book ai didi

Spring Security 4 和 JSF 2 集成

转载 作者:行者123 更新时间:2023-12-02 23:04:11 29 4
gpt4 key购买 nike

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

我找到了this neat thing它允许您将 Spring Boot 和 JSF 2 与 PrimeFaces 5 混合使用。很棒的东西。我想看看你是否可以将其提升到另一个水平。

通常您会像这样为 Spring MVC 配置 Spring Security:

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 Security 的决定性答案。您能否使用 JSF 实现前端,然后由 Spring Security 进行管理,例如链接,即: localhost:8080/home 而不是 localhost:8080/home.xhtml 得到正确的管理和服务?并且WebSecurityConfig.java中定义的用户级别只能访问与自己相关的页面。

根据我(简要)调查,这可能是不可能的,因为 Faces 和 Mvc 是不同的技术,不能很好地协同工作。不过,如果可能的话我想确认一下是否可行。

如果可能的话,您能否提供一个工作示例,或者更深入的链接?我用谷歌搜索了很多次,但 100% 可能我最终错过了一些东西。

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

最佳答案

一起使用 Spring Boot、Spring Security、JSF 和 Spring Core 没有问题,最终,JSF View 被解析为 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 Security 4 和 JSF 2 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789208/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com