简体   繁体   English

如何将用户注册表格与Spring Security集成在一起

[英]How to intergrate user registration form with spring security

I am using Spring Framework and Spring security in my web application. 我在Web应用程序中使用Spring Framework和Spring安全性。 I want to integrate a user registration form to the front-end. 我想将用户注册表格集成到前端。 I created the jsp page and the controller and linked the page to Home page. 我创建了jsp页面和控制器,并将页面链接到Home页面。 But the text box fields in the registration form are disabled when navigate to that page. 但是,当导航到该页面时,注册表单中的文本框字段将被禁用。 I want know that spring-security is responsible for this and if yes what are the configurations I have to made in spring-security.xml 我想知道spring-security对此负责,如果是的话,我必须在spring-security.xml中进行哪些配置?

Thanks. 谢谢。

Here's my code 这是我的代码

JSP page JSP页面

<jsp:include page="header-form.jsp">
<jsp:param name="title" value="Customer"/>
</jsp:include>

<body>
<div id="tableContainer-1">
<div id="tableContainer-2">
    <form:form id="frmEdit" method="post" form action=""  modelAttribute="customer">
    <legend>Personal Information</legend>
        <div class="form-group">
            <label class="control-label">First Name</label>
            <form:input path="firstName" class="form-control" type="text" required="true" size="40" maxlength="10"/>
        </div>
        <div class="form-group">
            <label class="control-label">Last Name</label>
            <form:input path="lastName" class="form-control" type="text" required="true" size="40" />
        </div>
        <c:if test="${screenMode == 'add'}"> 
            <div class="form-group">
                <label class="control-label">Country</label>

                <form:select path="countryCode.id" id="countrylist" class="form-control">
                          <option value="">Select</option>
                          <form:options items="${countryList}" itemValue="id" itemLabel="countryDesc" />
                </form:select>
            </div>
        </c:if>
        <div class="form-group">
            <label class="control-label">Contact No</label>
            <form:input path="contactNo" class="form-control" type="text" required="true" size="40" />
        </div>

        <div class="form-group">
            <label class="control-label">Email</label>
            <form:input path="email" class="form-control" type="text" required="true" size="40" />
        </div>


        <div class="form-group">
            <label class="control-label">NIC / PP / DL</label>
            <form:input path="nicPpDl" class="form-control" type="text" required="true" size="40" />
        </div>


        <legend>Login Details</legend>

        <div class="form-group">
            <label class="control-label">User Name</label>
            <form:input path="userName" class="form-control" type="text" required="true" size="20" />
        </div>


        <div class="form-group">
            <label class="control-label">Pass Word</label>
            <form:input path="password" class="form-control" type="text" required="true" size="20" />
        </div>

        <div class="form-group">
            <label class="control-label">Confirm PassWord</label>
            <form:input path="" class="form-control" type="text" required="true" size="40" />
        </div>


        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn" onclick="onCancel()">Cancel</button>
        </div>      
    </form:form>

</div>
</div>
</body>

spring-security.xml 弹簧security.xml文件

Note that /cus/customer/list is the url for registration form. 请注意,/ cus / customer / list是注册表格的网址。

<?xml version="1.0" encoding="UTF-8"?>
<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">

<global-method-security secured-annotations="enabled" />



<!-- <http pattern="/cus/welcome/" security="none" />-->

<http pattern="/cus/" security="none" />
<http pattern="/cus/about" security="none" />
<http pattern="/cus/service" security="none" />
<http pattern="/cus/agent" security="none" />
<http pattern="/cus/contact" security="none" />
<http security="none" auto-config="true" use-expressions="true" pattern="/static/**" />
<http security="none" auto-config="true" use-expressions="true" pattern="/cus/customer/**" />
<http auto-config="true" use-expressions="true">


    <intercept-url pattern="/cus/customer/list"  access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <form-login 
          login-page="/cus/" default-target-url="/index.jsp" always-use-default-target="true"
            authentication-failure-url="/cus/"
            />

   <!-- <logout
            invalidate-session="true" 
           logout-success-url="/cus/" 
        logout-url="/cus/"/> -->

</http>


<authentication-manager>
  <authentication-provider>
    <user-service>
        <user name="rajith" password="123" authorities="ROLE_USER" />

    </user-service>
  </authentication-provider>
</authentication-manager>
<authentication-manager> 
   <authentication-provider user-service-ref="customUserDetailsService">
        <password-encoder ref="passwordEncoder"/> 
    </authentication-provider>
</authentication-manager> 

<beans:bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>

<beans:bean id="customUserDetailsService" class="com.ontag.mcash.customer.web.service.impl.UserDetailsServiceImpl"/> 




</beans:beans>

Finally I resolved the issue. 最后,我解决了这个问题。 The issue was with the spring security. 问题在于春季安全。 The Spring security is restricting the access to the ajax call as it is not configured in the spring-security.xml to allow access. Spring安全性限制对ajax调用的访问,因为未在spring-security.xml中将其配置为允许访问。 You need to set security to none to every page and ajax call which you are requesting.just added following two lines to the spring-security.xml and all worked fine. 您需要为每个请求的页面和ajax调用将安全性设置为“无”。只需将以下两行添加到spring-security.xml即可,并且一切正常。

<http pattern="/cus/" security="none" />
<http pattern="/cus/about" security="none" />
<http pattern="/cus/service" security="none" />
<http pattern="/cus/agent" security="none" />
<http pattern="/cus/contact" security="none" />
<http pattern="/cus/customer/signup" security="none" />
<http pattern="/cus/customer/register" security="none" />
<http pattern="/cus/customer/login" security="none" />
<http pattern="/cus/customer/validatePassword" security="none" />
<http pattern="/cus/customer/test.json" security="none" />
<http pattern="/cus/agent/list.json" security="none" />
<http security="none" auto-config="true" use-expressions="true" pattern="/static/**"     />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM