Logout with Spring Security with Java configuration - spring

I am using Spring Security 4.0.2.RELEASE with Spring 4.2.0.RELEASE.
I am unable to create a logout link (I maen what must be the value of the href attribute).
Consider :
Configuring DelegatingFilterProxy in Java with WebApplicationInitializer:
public class SecurityWebInitializer
extends AbstractSecurityWebApplicationInitializer {
}
Simple configuration class to enable web security for Spring MVC
#Configuration
#EnableWebSecurity
public class SecurityConfig
extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().and()
.authorizeRequests()
.antMatchers("/spitter/").authenticated()
.antMatchers(HttpMethod.GET, "/spitter/register").authenticated().and()
.logout().deleteCookies("remove")
.invalidateHttpSession(true).logoutUrl("/logout")
.logoutSuccessUrl("/");
}
#Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password")
.roles("USER").and().withUser("admin").password("password")
.roles("USER", "ADMIN");
}
}
Controller:
#Controller
#RequestMapping(value = "/spitter")
public class SpittrController {
private SpittleRepository spittleRepository;
#Autowired
public SpittrController(SpittleRepository spittleRepository) {
this.spittleRepository = spittleRepository;
}
#RequestMapping(value = "/register", method = RequestMethod.GET)
public String showRegistrationForm() {
return "registerForm";
}
#RequestMapping(value = "/register", method = RequestMethod.POST)
public String processingRegistration(#Valid Spitter spitter, Errors errors) {
if (errors.hasErrors()) {
return "registerForm";
}
spittleRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUserName();
}
#RequestMapping(value = "/{username}", method = RequestMethod.GET)
public String showSpitterProfile(#PathVariable("username") String username,
Model model) {
Spitter spitter = spittleRepository.findByUsername(username);
if(spitter != null){
model.addAttribute(spitter);
}
return "profile";
}
}
registerForm.jsp:
<form method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastName" /></td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Register" /></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
</form>
After submission of registerForm.jsp, the profile.jsp is shown to the user:
profile.jsp:
<body>
<h1>Hello world!</h1>
<p>The time on the server is ${serverTime}.</p>
<h1>Your Profile</h1>
<h1>Logout</h1>
<table>
<tr>
<td>First Name:</td>
<td><c:out value="${spitter.firstName}" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><c:out value="${spitter.lastName}" /></td>
</tr>
<tr>
<td>User Name:</td>
<td><c:out value="${spitter.userName}" /></td>
</tr>
</table>
</body>
When I hit
http://localhost:8080/web/spitter/register
I am redirected to the login page. After login and submitting the form, the profile.jsp is shown in which I have included a Logout link. On clicking that, HTTP 404 comes up.
I have gone through Spring Security docs, but they have taken thymeleaf into consideration. My is a simple JSP page.
Furthermore, I have also considered taking this into account,
By default POST request is required to the logout url. To perform
logout on GET request you need:
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
1:
http://docs.spring.io/spring-security/site/docs/3.2.x/guides/hellomvc.html
Any suggestions?

Update the your code in profile.jsp as
<h1>logout</h1>
<c:url var="logoutUrl" value="/logout" />
<form action="${logoutUrl}" method="post" id="logoutForm">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>

Related

Why Http status code 404 shows when I click on submit using war file deployed

Image 1. Successful GET request
Image 2. Failed POST when click on submit
Welcome page i.e. index.jsp aprears on localhost:8080/war_filename
but when we click on login in index page it shows http status code 404
Is anyone there, who's having the same issue?
I've copied the war file in apache\webapps
WebController.java
#Controller
public class indexController {
String errorMessage = "Invalid credentials";
#GetMapping("/login")
public String login() {
return "index.jsp";
}
/*********** Login using form ************/
#RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(#RequestParam("firstname") String firstName,
#RequestParam("lastname") String lastName) {
System.out.println("inside login form");
Customer object = repository.findByFirstNameAndLastName(firstName,
lastName);
if (object != null) {
return "home.jsp";
}
return "index.jsp";
}
}
index.jsp
<h3>Do Login Here</h3>
<form method="POST" action="/login">
<table>
<tr>
<td>Employee ID</td>
<td><input type="text" name="firstname" placeholder="Username" required></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="lastname" placeholder="Password" required></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
home.jsp
<body>
<h3>Operations</h3>
<form action="/selectusingform" method="get">
Select
</form>
<table>
<tr>
<td>Insert</td>
</tr>
<tr>
<td>Update</td>
</tr>
<tr>
<td>Delete</td>
</tr>
<tr>
<td>Find</td>
</tr>
</table>

Spring MVC Form Validation Neither BindingResult nor plain target object for bean name 'BanqueForm' available as request attribute

I have a probleme when validating a form with spring MVC
i have this exception
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'BanqueForm' available as request attribute
this is my controller
package org.gestion.banque.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.gestion.banque.entities.Compte;
import org.gestion.banque.metier.IBanqueMetier;
#Controller
public class BanqueController {
#Autowired
private IBanqueMetier metier;
#RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model){
model.addAttribute("BanqueForm", new BanqueForm());
return"banque";
}
#RequestMapping(value="/chargerCompte", method=RequestMethod.GET)
public String charger(Model m) {
m.addAttribute("BanqueForm", new BanqueForm());
return "banque";
}
#RequestMapping(value="/chargerCompte",method=RequestMethod.POST)
public String charger(#Valid BanqueForm bf,
BindingResult result,Model model){
if(result.hasErrors())
{
return "banque";
}
try {
Compte c=metier.ConsulterCompte(bf.getCode());
bf.setTypeCompte(c.getClass().getSimpleName());
bf.setCompte(c);
} catch (Exception e) {
bf.setException(e.getMessage());
}
model.addAttribute("BanqueForm", bf);
return "banque";
}
}
and this is my view
<body>
<div>
<f:form modelAttribute="BanqueForm" method="post" action="chargerCompte" >
<table>
<tr>
<td>Code :</td>
<td><f:input path="code"/></td>
<td><f:errors path="code"></f:errors> </td>
</tr>
<tr>
<td>
<input type="submit" value="OK" />
</td>
</tr>
</table>
</f:form>
</div>
<c:if test="${ not empty BanqueForm.exception}">
<div>${BanqueForm.exception} </div>
</c:if>
<c:if test="${not empty BanqueForm.compte}">
<div>
<table>
<tr>
<td>Solde :</td>
<td>${BanqueForm.compte.solde}</td>
</tr>
<tr>
<td>Solde :</td>
<td>${BanqueForm.compte.dateCreation}</td>
</tr>
<tr>
<td>Type de Compte :</td>
<td>${BanqueForm.typeCompte}</td>
</tr>
<c:if test="${BanqueForm.typeCompte=='CompteCourant'}">
<tr>
<td>Decouvert :</td>
<td>${BanqueForm.compte.decouvert}</td>
</tr>
</c:if>
<c:if test="${BanqueForm.typeCompte=='CompteEpargne'}">
<tr>
<td>taux :</td>
<td>${BanqueForm.compte.taux}</td>
</tr>
</c:if>
</table>
</div>
</c:if>
</body>
</html>
Add #ModelAttribute("BanqueForm") annotation when obtaining BanqueForm object. Something like this:
public String charger(#Valid #ModelAttribute("BanqueForm") BanqueForm bf,
BindingResult result, Model model) { ... }

Spring:Using Controller actions in one JSP file

I am working on a Spring-MVc project. I have a single JSP page, with two forms. Both these forms are handled by 2 different controllers, and they have separate database tables, seperate service methods. But I would like to individually select information(notes) which the user is adding and save them. I am posting both of my controller, JSP file, and the error message. Kindly let me know what might be going wrong. Thank you for your time.
PersonController :
#Controller
public class PersonController {
private PersonService personService;
#Autowired(required=true)
#Qualifier(value="personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
//For add and update person both
#RequestMapping(value= "/person/add", method = RequestMethod.POST)
public String addPerson(#ModelAttribute("person") Person p){
//new person, add it
this.personService.addPerson(p);
return "redirect:/";
}
#RequestMapping("/remove/{id}")
public String removePerson(#PathVariable("id") String id){
this.personService.removePerson(id);
return "redirect:/persons";
}
#RequestMapping("/edit/{id}")
public String editPerson(#PathVariable("id") String id, Model model){
model.addAttribute("person", this.personService.getPersonById(id));
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
}
keyactivitiesController.java
#Controller
public class KeyActivitiesController {
private KeyActivitiesService keyActivitiesService;
#Qualifier(value="keyActivitiesService")
public void setKeyActivitiesService(KeyActivitiesService keyActivitiesService){this.keyActivitiesService = keyActivitiesService;}
#RequestMapping(value = "/keynotice", method = RequestMethod.GET)
public String listNotices(Model model) {
model.addAttribute("keyactivities", new KeyActivities());
model.addAttribute("listNotices", this.keyActivitiesService.listNotices());
return "keyactivities";
}
//For add and update person both
#RequestMapping(value= "/keynotice/add", method = RequestMethod.POST)
public String addPerson(#ModelAttribute("keyactivities") KeyActivities p){
//new person, add it
this.keyActivitiesService.addKeyNotice(p);
return "redirect:/";
}
#RequestMapping("/removeNotice/{id}")
public String removePerson(#PathVariable("id") String id){
this.keyActivitiesService.removeNotice(id);
return "redirect:/";
}
#RequestMapping("/editNotice/{id}")
public String editPerson(#PathVariable("id") String id, Model model){
model.addAttribute("keyactivities", this.keyActivitiesService.getNoticenById(id));
model.addAttribute("keyactivities", this.keyActivitiesService.listNotices());
return "keyactivities";
}
}
person.jsp
<c:url var="addAction" value="/person/add" ></c:url>
<form:form action="${addAction}" commandName="person">
<table>
<c:if test="${!empty person.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td colspan="1">
<c:if test="${!empty person.name}">
<input type="submit"
value="<spring:message text="Edit Notice"/>" />
</c:if>
<c:if test="${empty person.name}">
<input type="submit"
value="<spring:message text="Add Notice"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<c:url var="addAction" value="/keynotice/add" ></c:url>
<form:form action="${addAction}" commandName="keyactivities">
<table>
<c:if test="${!empty keyactivities.keynotice}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="keynotice">
<spring:message text="Keynotice"/>
</form:label>
</td>
<td>
<form:input path="keynotice" />
</td>
</tr>
<tr>
<td colspan="1">
<c:if test="${!empty keyactivities.keynotice}">
<input type="submit"
value="<spring:message text="Edit Notice"/>" />
</c:if>
<c:if test="${empty keyactivities.keynotice}">
<input type="submit"
value="<spring:message text="Add Notice"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Error :
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'keyactivities' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
refactor this method:
#RequestMapping(value = "/", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("keyactivities", new KeyActivities());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
The error is that after post either form, you will not have keyactivities in the model

How are Spring MVC Controllers being bound to JSP pages?

Hi I am new to spring and I am trying to develop a simple portlet that accepts users first and last name and saves it to db using hibernate.
Basically I cannot figure out how the jsps and controllers communicate; I am missing some chunk here.
This is my first controller that needs to be called (where do I mention so?)
package codes.controller;
import javax.portlet.RenderResponse;
import codes.base.User;
import codes.service.UserService;
#Controller(value="SimpleUserController")
#RequestMapping(value = "VIEW")
public class SimpleUserController {
// -- auto-wiring of service dependency
#Autowired
#Qualifier("userService")
private UserService userService;
// --maps the incoming portlet request to this method
#RenderMapping
public String showUsers(RenderResponse response) {
return "home";
}
#ExceptionHandler({ Exception.class })
public String handleException() {
return "errorPage";
}
// -- #ModelAttribute here works as the referenceData method
#ModelAttribute(value="user")
public User getCommandObject() {
return new User();
}
}
Initially I am displaying a home.jsp that will display the form with two input boxes and a submit button.
<%#include file="include.jsp" %>
<portlet:actionURL var="addUserActionUrl">
<portlet:param name="myaction" value="addUser" />
</portlet:actionURL>
<form:form name="home" commandName="user" method="post"
action="${addUserActionUrl}">
<table>
<tr>
<td>First Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="lastname" /></td>
</tr>
<table align="right">
<tr>
<td> </td>
<td><input type="submit" value="SUBMIT" /></td>
</tr>
</table>
</table>
</form:form>
This JSP should call the action method in the AddUserController.java:
package codes.controller;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import codes.base.User;
import codes.service.UserService;
#Controller(value = "AddUserController")
#RequestMapping(value = "VIEW")
public class AddUserController {
#Autowired
#Qualifier("userService")
private UserService userService;
#RenderMapping(params = "myaction=addUser")
public String showRegisterPage(Model model) {
model.addAttribute("user", new User());
model.addAttribute("users", getUsers());
return "addUser";
}
public List<User> getUsers() {
return userService.getAllUsers();
}
#ActionMapping(params = "myaction=addUser")
public void addBook(#ModelAttribute(value = "user") User user,
BindingResult bindingResult, ActionResponse response,
SessionStatus sessionStatus) {
if (!bindingResult.hasErrors()) {
userService.addUser(user);
response.setRenderParameter("myaction", "users");
sessionStatus.setComplete();
} else {
response.setRenderParameter("myaction", "addUser");
}
}
}
This time this firstname+last name should be saved in the db AND the screen should refresh to show a new form that will have a dropdown with the current users' names in the database and another first name and last name form fields. If you select a username from the dropdown the form fields are populated and you can edit these values and click on UPdate button to save the values in DB. Otherwise you can add a new user to the database using submit button.
addUser.jsp:
<%#include file="include.jsp" %>
<portlet:actionURL var="addUserActionUrl">
<portlet:param name="myaction" value="addUser" />
</portlet:actionURL>
<portlet:renderURL var="homeUrl">
<portlet:param name="myaction" value="Users" />
</portlet:renderURL>
<script type="text/javascript" src="js/userRelated.js"></script>
<form:form name="addUser" commandName="user" method="post"
action="${addUserActionUrl}">
<form:select path="model">
<form:option value="NONE" label="--- Select ---" id="userList" onchange="showHide()"/>
<form:options items="${users}" />
</form:select>
<table>
<tr>
<td>First Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name:<font style="color: #C11B17;">*</font></td>
<td><form:input path="lastname" /></td>
</tr>
<table align="right">
<tr>
<td> </td>
<td><input type="submit" id="submit" value="SUBMIT" />SUBMIT</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id="update" value="SUBMIT" />UPDATE</td>
</tr>
</table>
</table>
</form:form>
I am hiding and unhiding the SUBMIT/UPDATE button using onchange of dropdown. How do I call different functions in the addUsercontroller depending on the button available?
by updating the action attribute of form element with javascript

can someone explain how the controller class works

I am working on spring samples.I have a controller class named UserController2 as follows
#Controller
public class UserController2 extends MultiActionController {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
#RequestMapping(params = "add", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response, User user) throws Exception {
userDAO.saveUser(user);
return new ModelAndView("redirect:User.htm");
}
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("userList", userDAO.listUser());
modelMap.addAttribute("User", new User());
return new ModelAndView("userForm", modelMap);
}
}
and i have a jsp page called userForm.jsp
<form:form method="POST" action="add.htm" commandName="User" modelAttribute="User">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="password">Name</form:label></td>
<td><form:input path="password" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="gender">Name</form:label></td>
<td><form:input path="gender" /></td>
</tr>
<tr>
<td><form:label path="country">Name</form:label></td>
<td><form:input path="country" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
what actually does the below codes returns
return new ModelAndView("userForm", modelMap);
and
return new ModelAndView("redirect:User.htm");
I am getting the following error
java.lang.IllegalStateException: Neither BindingResult nor plain target object for
bean name 'User' available as request attribute
I searched but i cannot find a proper explanation for modelandview..
When using the modelAttribute make user that:1. Your method parameter user is annotated with #ModelAttribute("user")2. A method parameter of type BindingResult is declared.
Also, the annotation parameter value is case sensitive, and should exactly match the value of the modelAttribute you declared in userForm.jsp.

Resources