-1

spring 3.1 を使用して単純な spring mvc アプリケーションを作成しました。私の目的は、プロジェクトに spring-security 機能を実装することです。セキュリティ部分は正常に動作していますが、Spring コントローラークラス (Javaクラス)。

私はそれがjspで非常に簡単であることを知っています.request.getParameter( "input component name")を介してこれを達成していましたが、春なので、この構文を使用してコントローラー内の値を取得していないので、使用することにしました@RequestParam.私はここに私のコードを添付しています.私のjspページは次のようにlogin.jspです

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      <html>
        <head>
            <title>Login Page</title>
         <style>
           .errorblock {
            color:red;
            background-color: #ffEEEE;
            border: 3px solid #ff0000;
            padding: 8px;
            margin: 16px;
             }
        </style>
       </head>

        <body onload='document.f.j_username.focus();'>
                 <h3>Login with Username and Password (Custom Page)</h3>

<c:if test="${not empty error}">
    <div class="errorblock">
        Your login attempt was not successful, try again.<br /> Caused by:
        ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </div>
</c:if>

<form name='f' action="<c:url value='j_spring_security_check'/>"
    method='POST'>

    <table>
        <tr>
            <td>User:</td>
            <td><input type='text' name='j_username'>
            </td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type='password' name='j_password'/>
            </td>
        </tr>
        <tr>
            <td><input name="submit" type="submit"
                value="Submit" />
            </td>
            <td><input name="reset" type="reset" />
            </td>
        </tr>
    </table>
</form>
   </body>
  </html>

フィールド「j_username」から、コントローラー内でユーザーが入力した値を取得します。次のように、 ContactController.javaという名前のコントローラークラスをアタッチしています

   package com.edifixio.controller;

   import java.util.Map;
   import javax.servlet.http.HttpServletRequest;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.stereotype.Controller;
   import org.springframework.validation.BindingResult;
   import org.springframework.web.bind.annotation.ModelAttribute;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RequestMethod;
   import org.springframework.web.bind.annotation.RequestParam;
   import com.edifixio.model.Contact;
   import com.edifixio.service.InContactService;

   @Controller
   public class ContactController{

private InContactService inContactService;

public InContactService getInContactService() {
    return inContactService;
}

@Autowired
public void setInContactService(InContactService inContactService) {
    this.inContactService = inContactService;
}

@RequestMapping(value = "/index")
public String login() {
    return "login";
}

@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String loginError() {
    return "login";
}

@RequestMapping(value = "/logout")
public String logout() {
    return "login";
}

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String listManagers(Map<String, Object> map,@RequestParam String j_username){
    System.out.println("User="+j_username);
    map.put("contact", new Contact());
    map.put("contactList", inContactService.showAllManager());
    return "allcontact";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String storeManager(@ModelAttribute("contact") Contact contact,
        BindingResult bindingResult) {
    inContactService.addContact(contact);
    return "redirect:/index";
   }
   }

今、次のコントローラーコードでエラーが発生しています

   public String listManagers(Map<String, Object> map,@RequestParam String j_username){
    System.out.println("User="+j_username);
    map.put("contact", new Contact());
    map.put("contactList", inContactService.showAllManager());
    return "allcontact";
}

エラーが発生しています

   HTTP Status 400 - 

   type Status report

   description: The request sent by the client was syntactically incorrect ().

エラーを最適化するために次のコードを試しました::

   @RequestMapping(value = "/welcome", method = RequestMethod.GET)
  public String listManagers(Map<String, Object>map,@RequestParam(required=false)   String j_username){
    System.out.println("User="+j_username);
    map.put("contact", new Contact());
    map.put("contactList", inContactService.showAllManager());
    return "allcontact";
}

これを使用して、サーバー エラー 400 をバイパスできましたが、上記のコントローラー内でユーザー名を取得できませんでした

ここに私のspring-security.xmlファイルがあります

   <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.1.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.1.xsd">

  <http auto-config="true">
    <intercept-url pattern="/login" access="ROLE_ADMIN" />
    <form-login login-page="/login" default-target-url="/welcome"
        authentication-failure-url="/loginfailed" />
    <logout logout-success-url="/logout" />
  </http>

  <authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="SELECT user_name,user_password,account_status FROM systemuser WHERE user_name=?"
            authorities-by-username-query="SELECT user_name,authority FROM systemuser WHERE user_name=?"/>
    </authentication-provider>
</authentication-manager>
 </beans:beans>

誰もがこれに対する実行可能な解決策を持っていますか?????????

4

1 に答える 1

0

j_usernameとを処理する必要はありませんj_password。フォームが送信されたら、spring_security を使用して authentication-manager を使用し、ユーザー名とパスワードが有効かどうかを確認します。データが有効な場合は、ようこそにリダイレクトされます。そうしないと、ログインに失敗しました。ユーザー認証後にコントローラー内でユーザー名とパスワードが必要な場合は、Principal.

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String listManagers(Map<String, Object> map, Principal principal){
    System.out.println("User="+principal.getName());
    map.put("contact", new Contact());
    map.put("contactList", inContactService.showAllManager());
    return "allcontact";
}
于 2013-11-11T11:20:32.997 に答える