I am trying to implement spring safety features for authorization and certification. I have been facing some issues regarding user roles. To check in my controller, the user wants to log in that role, so depending on the role I can redirect the user to the related page (as a user gives more than one role). However, I can not find the role of logging in my Java controller.
Login jsp page
& lt; Form action = "& lt; c: url value = 'j_spring_security_check' / & gt; method =" post "name = 'login form' & gt; & Lt; Table class = "center" & gt; & Lt; Tr & gt; & Lt; Td> Username: & lt; / Td> & Lt; Td> & Lt; Input type = "text" name = "j_username" id = "user name" & gt; & Lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; Td> Password: & lt; / Td> & Lt; Td> & Lt; Input type = "password" name = "j_password" id = "password" & gt; & Lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; Td> Role: & lt; / Td> & Lt; Td> & Lt; Select id = "role" name = "j_role" & gt; & Lt; Option style = "font-weight: bold;" & Gt; Select Role & lt; / Option & gt; & Lt; Option id = "roll 1" value = "role 1" & gt; Role 1 & lt; / Options & gt; & Lt; Option id = "role 2" value = "role 2" & gt; Role 2 & lt; / Options & gt; & Lt; Option id = "roll 3" value = "role 3" & gt; Role 3 & lt; / Options & gt; & Lt; / Select & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; Tr & gt; & Lt; Td> & Lt; Input type = "submit" value = "login" style = "height: 25px; width: 100px" & gt; & Lt; / Td> & Lt; TD & gt; & Lt; Input type = "reset" value = "reset" style = "height: 25px; width: 100px" & gt; & Lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; TD colspan = 2 & gt; & Amp; Nbsp; & Lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; Td colspan = 2 & gt; & Lt; / Td> & Lt; / Tr & gt; & Lt; / Table & gt; & Lt; / Form & gt; Admin Model: ModelMap Model, Principal Principal ) SQLException throws {ModelAndView mav = new ModelAndView (); Try {authentication auth = SecurityContextHolder.getContext (). GetAuthentication (); String n = auth.getName (); String r = auth.getAuthorities (). ToString (); System.out.println ("value of user name" + n); System.out.println ("value of role" is + r); Returning results}}
Here the user has logged me in and I am retrieving my roles, as my spring security. XML file is defined in - e.g. [Roll 1, roll 2] It does not retrieve the role by which the user has logged in. After the user that logged in through that username, I get the role from the database and redirect to the related page. It works fine if the user has only one role, but if they have more than one role, then I am not clear how I will redirect them. So, I thought that once the user can play a role in the login and I will redirect.
Spring-protection. Xml
& lt; Http auto-config = "true" usage-value = "true" & gt; & Lt; Intercept-url pattern = "/ home" access = "enniol ('roll 1', 'roll 2', 'roll 3')" /> & Lt; Form-login login-page = "/ login" default-target-url = "/ home" authentication-failure- url = "/ loginError" /> & Lt; / Http & gt; & Lt; Authentication-manager aliases = "authentication manager" & gt; & Lt; Authentication Provider & gt; & Lt; User Service & gt; & Lt; Username = "user1" password = "123" officers = "role1, role 2" /> & Lt; Username = "user2" password = "123" officers = "role 2, role 3" /> & Lt; Username = "stephen" password = "123" officers = "role1" /> & Lt; Username = "robert" password = "123" officers = "role 3" /> & Lt; / User service & gt; & Lt; / Authentication Provider & gt; & Lt; / Authentication-manager & gt;
Each role has different watch pages, so if I login with user1 and dropdown, I will select role2. How do I get this role 2 in my controller so that I can redirect user 1 to the related JSP page? ?
If there is a better way of achieving this, please let me know.
instead of principal
> in your controller a Inject
authentication. @ Request Mailing (value = "/ home", method = RequestMethod GET) Public ModelAndView printWelcome (ModelMap Model, Authentication Authentication) {}
Authentication GetAuthorities ();
will now roll back your roles.
No comments:
Post a Comment