java - 使用 spring security java 限制用户在查看页面之前进行身份验证

标签 java html google-app-engine spring-security

我是 Spring 框架和 Spring 安全的新手。我正在使用谷歌应用引擎开发一个应用程序。我正在尝试对用户进行身份验证,但无法实现。我的问题是

  1. 我需要限制所有用户在浏览器中键入 URL 和查看页面。如果他们想要访问此类页面,我需要将他们导航到警告页面。
  2. 只有通过应用程序的身份验证,用户才能访问该应用程序。如果未通过身份验证,则应导航登录页面。
  3. 我需要编写一个自定义登录验证页面,如果给定的凭据是完美的,我应该在页面中验证它们,然后我们可以将它们导航到主页。
  4. 在自定义登录身份验证页面中,我们应该编写数据库逻辑以从数据库中获取凭据并进行身份验证。如果用户没有在应用程序中注册,那么我们应该使用默认消息将他们导航到注册页面。

请任何人给我一个符合此要求的默认应用程序。

感谢和问候, 查看

最佳答案

我使用的是spring 3.x来完成配置

在 web.xml 中,添加这些行

<filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

来到security xml页面添加这些。我正在使用 DAOAuthenticationProvider。我正在使用 BCryptPasswordEncoder 进行密码编码。

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security 
                    http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    <http use-expressions="true">
        <form-login login-page="/login" always-use-default-target="true" default-target-url="/sessionInit" authentication-failure-url="/login"/>
        <logout logout-url="/logout" logout-success-url="/logout"/> 
    </http>

    <beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService" ></beans:property>
    </beans:bean>

    <beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
        <beans:property name="providers">
            <beans:list>
                <beans:ref local="daoAuthenticationProvider"/>
            </beans:list>
        </beans:property>
    </beans:bean>

    <authentication-manager>
       <authentication-provider ref="authProvider"></authentication-provider> 
    </authentication-manager>

    <beans:bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService" />
        <beans:property name="passwordEncoder" ref="encoder" />
    </beans:bean>

      <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

</beans:beans>

在所有这些配置之前,您需要在 web.xml 中添加这个 security.xml 文件。

关于java - 使用 spring security java 限制用户在查看页面之前进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22804058/

相关文章:

java - 比较二维 boolean 数组值?

java - Room + RxJava 2 如何检查表中某列等于特定值的记录

html - float 后添加div

html - 您可以将类中的一个属性以不同的方式应用于两个元素的 DIV 吗?

python - Google App Engine - 是否有必要在处理程序中调用 self.response?

google-app-engine - 如何解决主要的应用引擎问题?

google-app-engine - 使用 Google App Engine 的大型多用户实时应用程序

java - 表单验证无法正常工作

html - Bootstrap 下拉菜单溢出 html 正文

java - '%' Tomcat字符解码异常