我正在尝试将密码值传递给spring-boot控制器,对此没有任何回应,有人可以对此提出建议吗?
我之前使用过Spring MVC控制器,这是我第一次尝试使用spring-boot
<form method="POST" th:action="@{/esparkUserPage}">
<div class="control-group">
<input id="password" type="password" name="password" class="form-control input-sm" required="User Pwd is Required" placeholder="password" />
<label class="login-field-icon fui-lock" for="login-pass"></label>
</div>
<input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" id="submit" name="submit" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
MVcConfig:
@Configuration
public class WbMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("esparkLoginPage");
registry.addViewController("/esparkHome").setViewName("esparkHome");
registry.addViewController("/esparkUserPage").setViewName("esparkUserPage");
registry.addViewController("/esparkLoginPage").setViewName("esparkLoginPage");
}
}
Securityconfig:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
//.antMatchers("/", "/esparkLoginPage","/passReset").permitAll()
.anyRequest()
.permitAll() //.authenticated()
.and()
.formLogin()
.loginPage("/esparkLoginPage")
.defaultSuccessUrl("/esparkUserPage")
.permitAll()
.and()
.csrf().disable()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/esparkLoginPage")
.permitAll();
}
resetcontrlloer
@Controller
public class ResetController {
@ResponseBody
@RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
public String esparkUserPage(HttpServletRequest httpRequest,HttpServletResponse response ) {
String username = httpRequest.getParameter("username");
String password = httpRequest.getParameter("password");
System.out.println(username);
List<String> result = new ArrayList<String>();
/* try
{
JSch jsch = new JSch();
System.out.println("Inside shell");
Session session = jsch.getSession(USERNAME, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
session.connect();
//create the excution channel over the session
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
System.out.println(channelExec.toString());
// Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
InputStream in = channelExec.getInputStream();
String com="sh set_passwd"+" "+username+" "+"'"+password+"'" ;
channelExec.setCommand(com);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
{
result.add(line);
}
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
session.disconnect();
if(exitStatus < 0){
}
else if(exitStatus > 0){
}
else{
}
}
catch(Exception e)
{
System.err.println("Error: " + e);
}*/
return "Password Reset Done!";
}
}
还请提供有关从spring-boot执行shell命令的最佳方法的建议吗? 首次使用spring-boot进行FYI。
答案 0 :(得分:0)
因此,您有一些选择:
1-如果您只需要将密码作为String传递,则可以在Controller中使用注释@RequestParam
来发送请求,因此控制器必须类似于:
@Controller
public class ResetController {
@ResponseBody
@RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
public String esparkUserPage(@RequestParam("password")String password, HttpServletRequest httpRequest,HttpServletResponse
response ) {
// your code
}
2-如果要为控制器发送对象,则可以采用以下一种方式: 创建资源来打开模板,这意味着,只需在登录页面上添加参考对象,就像这样:
//supose your login.html
@RequestMapping("/login")
public String loginPage(Model model) {
model.addAttribute("user", new User());
return "login";
}
然后确保模板正确绑定对象,如下所示:
<form method="POST" th:action="@{/esparkUserPage}" th:object="${user}">
<div class="control-group">
<input id="password" type="password" name="password" class="form-control input-sm" required="User Pwd is Required" placeholder="password" th:field="*{password}" />
<label class="login-field-icon fui-lock" for="login-pass"></label>
</div>
<input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" id="submit" name="submit" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
这是您的用户类别的模型:
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
现在在您的控制器上添加注释如何在服务器端绑定对象:
@ResponseBody
@RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
public String esparkUserPage(@ModelAttribute(name="user") User user, HttpServletRequest httpRequest,HttpServletResponse
response ) {
// your code
}
我认为这可以帮助您根据请求发送参数。