我正在使用Spring Security 4.1.1,但遇到一个问题:我尝试访问URL,应用程序重定向到登录页面。到现在为止还挺好。
但是,成功登录后,应用程序会再次将我重定向到登录页面,并且不会创建任何会话,因此即使尝试直接访问URL(在URL栏上键入),应用程序也会重定向到登录页面。
我必须要输入一些URL才能访问它们。其他人,我无需身份验证即可访问。这些我不需要身份验证的URL可以很好地工作(即,无需登录并访问该URL),但是那些需要身份验证的URL要求登录,但是在键入用户名和密码后,它将再次重定向到登录页面,然后我无法直接访问该URL。
我的configure()方法是这样的:
// Those URL pattern I need authentication to access them.
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/gerenciar/**", "/admin/adicionar/**", "/admin/index.html", "/admin/").
authenticated().and().formLogin().loginPage("/admin/login.html").permitAll();
}
我不确定是否需要发布任何其他代码来澄清我的问题或帮助其他人回答我。如果是这样,请告诉我,以便我更新我的问题。
编辑1
一个问题:我想在antMatchers()方法参数中要求对那些URL进行身份验证:是否需要编写“ /admin/index.html”和“ / admin /”?Spring Security是否一样?
编辑2
我想让所有人访问的URL:“ /”和“ / admin / resources / **”。在/ admin / resources内部,我有CSS和js,这就是为什么我希望每个人都可以访问它们。这些URL无需身份验证。
我需要人们对URL进行身份验证:“ / admin / gerenciar / **”,“ / admin / adicionar / **”和“ / admin /”(当然是暗指“ /admin/index.html”)。
我现在的问题是:如何为该代码编写代码(在configure()方法内部)?
我的理解是-
任何人都可以访问/,admin / login.html和/ admin / resources / **,但是其他admin网址不可以吗?
我认为方法应该是-
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**", "/admin/login.html", "/admin/resources/**").permitAll()
.antMatchers("/admin/**", "/admin/gerenciar/**", "/admin/adicionar/**").
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/admin/login.html")
.permitAll();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句