我使用弹簧3.1制作了一个简单的弹簧mvc应用程序。我的目标是在我的项目中实现弹簧安全功能。安全部分工作正常但是我在获取用户在我的弹簧控制器中输入的用户名时遇到问题class(Java类)。
我知道用jsp很容易,我们通过request.getParameter(“输入组件名称”)来实现这一点 但是由于它是春天,我没有使用这种语法在我的控制器中得到值,所以,我决定使用@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'的我们想要获取用户在我们的控制器中输入的值。现在我将我的Controller类名为 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>
任何人都可以有任何可行的解决方案吗?????????
答案 0 :(得分:0)
您无需处理j_username
和j_password
。提交表单时,spring_security使用authentication-manager检查用户名和密码是否有效。如果数据有效,您将被重定向到welcome。否则登录失败。如果您在用户身份验证后需要控制器内的用户名和密码,则可以使用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";
}