【问题标题】:authentication spring security身份验证弹簧安全
【发布时间】:2018-11-11 16:35:43
【问题描述】:

我正在尝试使用 spring security 和 jsf 进行身份验证,但是当我输入身份验证信息时它不起作用。 我尝试了一些解决方案,这是一个主题,这是我第一次配置 jsf,spring security

这是我的代码:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;

@Autowired
public void configurationGlobal(AuthenticationManagerBuilder auth) throws  Exception {
       auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
   // auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().
            authorizeRequests()
                .anyRequest()
                      .authenticated()
            .and()
            .formLogin()
                 .loginPage("/login.xhtml").loginProcessingUrl("login").failureUrl("/login.xhtml?error=true")
                      .permitAll()
            .defaultSuccessUrl("/index.xhtml");

}

@Bean
public BCryptPasswordEncoder passwordEncoder() {
 return new BCryptPasswordEncoder();
};
}

这是我的登录页面

      <form action="${request.contextPath}/login" method="POST">
        <h:outputLabel value="Enter UserName:" />
        <input type="text" name="username"/><br/><br/>
        <h:outputLabel value="Enter Password:" />
        <input type="password" name="password"/> <br/><br/>         
        <input type="submit" value="Login"/>
        <input type="hidden" name="${_csrf.parameterName}"   value="${_csrf.token}"/>           
      </form>

faces-config.xml

  <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.2"
          xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
      <application>
    <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
   </application>
 </faces-config>

这是我的 web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
    org.springframework.web.context.request.RequestContextListener
</listener-class>
 </listener>
</web-app>

【问题讨论】:

  • 此问题与 jsf 无关...请详细说明您认为您的问题与 jsf 相关的原因?
  • 我正在使用 jsf,但通过身份验证,我发现这是一种解决方案,我把它放在这里,我尝试了一下,但没有用
  • 不是 spring jsf 解决方案。它是普通的 jsp/spring/el。我认为您在某处误解了教程。
  • 我正在寻找解决方案,我将把jsf配置
  • 抱歉,不清楚的地方

标签: spring spring-security


【解决方案1】:

Spring boot 是一个独立的服务器,JSF 是一个 WEB 前端框架。首先,您应该将两者结合起来。为了便于开始使用https://github.com/joinfaces/joinfaces

【讨论】:

  • JSF 不需要使用 springboot。您可以单独使用它们。
  • 当然 JSF 本身不需要 Spring 或 Spring boot。 Sprig boot 可以用作后端服务,也可以将 JSF 与 Spring 集成并部署在 Tomcat 下,或者将 JSF 与 Spring boot 集成并作为独立的 Web 应用程序运行。
【解决方案2】:

我通过删除 passwordEncoder 来解决问题

 @Bean
 public BCryptPasswordEncoder passwordEncoder() {
   return new BCryptPasswordEncoder();
  };

因为我使用的是 spring security 5,所以我在密码前添加了 {noop},就像这样

 auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN");

【讨论】:

  • 很好,你解决了它,但问题中仍然没有与 jsf 相关的内容(发布 faces 配置不会使 jsf 成为问题的一部分),也没有答案。而且不使用 pw 编码器似乎很危险
  • 我之前用 spring MVC 做的一些事情的问题,当我用 jsf 尝试它时,每件事都正常工作,我认为这是 JSF 问题,谢谢你 -1跨度>
  • -1 不是我的,虽然我认为不使用密码编码器的解决方案非常糟糕,这不应该是 对您的问题的回答)
猜你喜欢
  • 2012-02-15
  • 2015-09-04
  • 2021-06-05
  • 2017-09-22
  • 2020-06-21
  • 2014-05-20
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多