gpt4 book ai didi

java - 在 Spring Boot 启动器安全性中使用自定义登录页面成功登录,但无法进入下一页

转载 作者:行者123 更新时间:2023-12-01 21:43:58 27 4
gpt4 key购买 nike

我一直在研究Spring Boot。当将 spring-boot-starter-security 用于待办事项应用程序时,我尝试使用自定义用户 ID 和密码以及自定义登录页面登录。当我尝试登录时,它不会带我进入下一页。注意:用户名是下一页的必需参数。

我尝试使用下面的方法,但登录后它再次将我带到登录页面作为错误

@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();
}
}
}
}

我的期望是使用用户名登录应用程序并获取用户的待办事项

最佳答案

尝试添加 loginProcessUrldefaultSuccessUrl。像这样的事情:

.formLogin()
.loginPage("/login")
.loginProcessingUrl("/do_login")
.defaultSuccessUrl("/index")

此示例中的索引是您希望在成功登录后进入的页面。

关于java - 在 Spring Boot 启动器安全性中使用自定义登录页面成功登录,但无法进入下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com