- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法集成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/
我试图了解 JSF 实现如何识别用户的各种可能操作。在我放在一起的简单应用程序中,我在 login.xhtml 页面中配置了以下字段。 用户名 - 输入字段 密码 - 密码字段 登录按钮 取消按钮 登
我已经开始学习 JSF,我想知道在我们的类路径中包含什么 JAR 以开始使用 JSF。是jsf-api或 jsf-impl ?或者我们必须同时包含两者?如果两者都是,那么为什么它们不合并? 最佳答案
我是 java server faces (JSF) 的初学者,我需要将文本输入的内容传递到第二页以显示它,同样适用于第二页:我想将单选按钮值传递到第三页。我搜索并尝试了很多但没有成功。例如我试过
我有一个 JSF 页面。我的 CommandButton 操作方法值取决于 bean 变量值。 例子: Bean headerBean 具有可变的 actionValue,值为“someBean.do
我有两个 JSF 页面,假设 A 和 B。从这两个页面 A 和 BI 可以导航到页面 C。现在页面 C 中有一个按钮(确定按钮),单击它应该导航回 A 或 B,具体取决于从哪里(A 或 B)调用 C
我可以在没有 JSTL 标签的情况下使用 JSF 执行条件逻辑吗? 例如,我制作了一个复合组件,并想说明,如果指定了“id”属性,则定义“id”属性,但如果未指定“id”属性,则不要指定“id”属性。
我有一个应用程序,用户可以在其中从我的应用程序的欢迎页面动态切换语言环境。我看到早期的开发人员(在没有太多文档的情况下继承了代码)已经从 ViewHandler 覆盖了以下三个方法,并告诉我这是动态切
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
有没有一种方法可以在我的 JSF 2.0 应用程序中处理浏览器刷新事件,以便在浏览器刷新页面时将用户导航到欢迎页面? 这让我想到了另一个问题,即如何在托管 bean 中进行页面导航? 干杯, 最佳答案
我有两页。搜索页面是第一个接受用户输入的页面。第二页显示数据表中的结果集。第二页有 3 个面板,用于结果集、更新和在单个页面中创建。根据单击的按钮,我将面板呈现为真和假。 . . . . . .
由于我们在 Asp.Net 中有 comparevalidator,我们在 JSF 中有什么来验证两个字段的值是否相同?我想验证密码和确认密码字段。 最佳答案 不,这样的验证器在基本的 JSF 实现中
我想构建一个自定义 JSF 组件。现在我从 oracle 阅读了一些文档并看到了一些代码示例。问题是我有点困惑: 似乎有两种方法可以使用 JSF 2.0+ 构建自定义组件。据我了解,自 JSF 2.0
我遇到了与 user1598186 在他的问题中提到的相同的问题:p:commandButton doesn't call bean's method in an page 但是,没有给出解决方案(
这个问题在这里已经有了答案: Ajax update/render does not work on a component which has rendered attribute (1 个回答)
是否有内置机制可以有条件地重定向到另一个 View ?如果他/她已经登录,我希望用户从登录页面重定向到“主页”。 我已经有两种基本方法,但对于第一种我不知道如何实现,第二种是一种肮脏的解决方法。 添加
如何在 JSF 中格式化值 我需要格式化一个数字,如:12345.67 到 12,345.67 可以用模式吗? 最佳答案 尝试使用: 关于jsf - 用逗号格式化为数字 JSF,我们在Sta
根据this blog JSF 正在走向无状态。使用 JSF 的全部意义不在于它使保存和恢复状态成为一件苦差事。 JSF 成为无状态的有什么意义?您能否提供一个有用的示例。 最佳答案 首先,我想澄清
我读到某个地方(不再找到它),可以在资源包中使用EL Expresions,然后在不更改* .xhtml文件的情况下使用它。 some.text=#{someBean.stepsLeft} more
我想看一个简单的登录应用程序,不像this那么简单尽管。 我想要实现的是对 JSF 的工作原理的理解,我开发了很多 ASP.NET,您可以在其中隐藏代码,并且您可以在其中检查是否在登录时创建了 ses
如果#{myBean.birthdate}是java.util.Calendar或java.util.Date类型,我可以格式化吗this 在 EL 本身内部可能使用现有函数,其输出类似于 DateF
我是一名优秀的程序员,十分优秀!