使用Spring Boot Starter安全性中的Custom登录页面成功登录,没有进入下一页
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring Boot Starter安全性中的Custom登录页面成功登录,没有进入下一页相关的知识,希望对你有一定的参考价值。
我一直在研究弹簧靴。在将tom应用程序使用spring-boot-starter-security时,我尝试使用自定义的用户名和密码以及自定义的登录页面进行登录。当尝试登录时,它并没有带我进入下一页。注意:用户名是下一页的必需参数。
我尝试在下面使用,但登录后再次使我以登录页面为错误
@Override
protected void configure(HttpSecurity http) throws Exception
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/", "/*Todo*/**").access("hasRole('USER')").and()
.formLogin().loginPage("/login").permitAll();
这是我的securityConfig代码
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/listTodo").hasAnyRole("USER","ADMIN")
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll().and()
.logout().permitAll();
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
auth.inMemoryAuthentication().withUser("Sudhakar").password("qwe123").roles("USER","ADMIN");
I would need to login with user name and get the todo details of the user who logged in. But I am not able to get to next page, after trying many times I am getting below error
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
Below is my controller
package com.example.SpringLogin.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.example.SpringLogin.service.loginService;
@Controller
@SessionAttributes("name")
public class LoginController
@Autowired
loginService service;
@RequestMapping(value="/login",method = RequestMethod.POST)
public String loginMessage(ModelMap model,@RequestParam String name,@RequestParam String password)
boolean isValidUser=service.validateUser(name, password);
if(!isValidUser)
model.put("message", "Invalid Credentials");
return"Room";
model.put("name", name);
model.put("password",password);
return "redirect:/todoList";
@RequestMapping(value="/login",method = RequestMethod.GET)
public String roomLogin(ModelMap model, String error)
//model.put("name", name);
if(error!=null)
model.addAttribute("errorMsg","UserName or Password is invalid");
return "Room";
/*@RequestMapping(value="/login",method = RequestMethod.GET)
public String showLogin(ModelMap model)
//model.put("name", name);
return "Welcome";
*/
@RequestMapping(value = "/welcome")
public String showWelcome(ModelMap model)
return "login";
My login page
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="contextPath" value=""/>
<!DOCTYPE html>
<html>
<head>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
<title>Todo Application</title>
</head>
<body>
<div class="container">
<font color="red">$message</font>
<form:form method="post" action="/login">
<fieldset class="form-group">
Name : <input type="text" name="username" class="form-control" placeholder="Username"
autofocus="true"/>
Password: <input type="password" name="password"
class="form-control" placeholder="Password" />
</fieldset>
<button type="submit" class="btn btn-success">Submit</button>
</form:form>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
After successful login it should go to below page
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="contextPath" value=""/>
<!DOCTYPE html>
<html>
<head>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
<title>Todo Application</title>
</head>
<body>
<div class="container">
<table class="table table-striped">
<H1>Name : $pageContext.request.userPrincipal.name</H1>
<thead>
<tr>
<th>Id</th>
<th>Course</th>
<th>End Date</th>
<th>Is it Done</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach items="$todo" var="item">
<tr>
<td>$item.id</td>
<td>$item.course</td>
<td><fmt:formatDate value="$item.date" pattern="MM/dd/yyyy" /></td>
<td>$item.isdone?'Yes':'No'</td>
<td><a type="button" class="btn btn-success"
href="/update-Todo?id=$item.id">Update</a></td>
<td><a type="button" class="btn btn-warning"
href="/delete-Todo?id=$item.id">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<div>
<a type="button" href="/add-Todo" class="btn btn-success">Add a
Todo</a>
</div>
</div>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
Service class
package com.example.SpringLogin.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;
import com.example.SpringLogin.model.todoList;
@Service
public class TodoService
public static List<todoList> todos=new ArrayList<todoList>();
public static int todoCount=5;
static
todos.add(new todoList(1, "Sudhakar", "Study history", new Date(), false));
todos.add(new todoList(2,"Sudhakar","Study geography",new Date(),false));
todos.add(new todoList(3,"Sudhakar","Study GK",new Date(),false));
todos.add(new todoList(4,"Mani","Study Java",new Date(),false));
todos.add(new todoList(5,"Mani","Study script",new Date(),false));
public List<todoList> retrievetodos(String name)
List<todoList> retrieved=new ArrayList<todoList>();
for (todoList todo : todos)
if(todo.getName().equalsIgnoreCase(name))
retrieved.add(todo);
return retrieved;
public void addTodo(String name,String Course,Date date,boolean isDone)
todos.add(new todoList(++todoCount,name,Course,date,isDone));
public todoList retrieveTodo(int id)
for (todoList todo : todos)
if(todo.getId()==id)
return todo;
return null;
public List<todoList> UpdateTodo(todoList todo)
/*for (todoList td : todos)
if(td.getId()==todo.getId())
td.setCourse(todo.getCourse());
td.setDate(todo.getDate());
*/
todos.remove(todo);
todos.add(todo);
return todos;
//it will delete the todo
public void deleteTodo(int id)
Iterator<todoList> it = todos.iterator();
while(it.hasNext())
todoList td=it.next();
if(td.getId()==id)
it.remove();
我的期望是使用用户名登录应用程序并获取用户的待办事项列表
答案
尝试添加loginProcessUrl
和defaultSuccessUrl
。像这样的东西:
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/do_login")
.defaultSuccessUrl("/index")
此示例中的索引是成功登录后要转到的页面。
另一答案
使用此一个
.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/perform_login")
.defaultSuccessUrl("/homepage.html", true)
以上是关于使用Spring Boot Starter安全性中的Custom登录页面成功登录,没有进入下一页的主要内容,如果未能解决你的问题,请参考以下文章
关于spring boot启动监控端点的方法(spring-boot-starter-actuator)
寻找 Spring Boot Hystrix Dashboard 解释(Spring Boot Starter)安全性的解决方案 Hystrix Stream(作为它自己的项目)?
在spring boot starter安全项目中看不到默认密码