reset password
Author Message
talisunep
Posts: 140
Posted 21:24 May 28, 2013 |

Professor in the case we want spring security to authenticate using the user email and password instead of its default username and password and check how can we implement this?

cysun
Posts: 2935
Posted 21:50 May 28, 2013 |

Spring Security's default schema for authentication uses a users table and an authorities table, but it's OK if your schema is different - in this case you'd have an email column instead of a username column. You just need to specify the users-by-username-query attribute and/or the authorities-by-username-query attribute of <jdbc-user-service>. See http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#nsa-jdbc-user-service

talisunep
Posts: 140
Posted 23:12 May 29, 2013 |

professor for hw6 i was wondering do we need to be able to register administrators too? 

cysun
Posts: 2935
Posted 08:38 May 30, 2013 |
talisunep wrote:

professor for hw6 i was wondering do we need to be able to register administrators too? 


No, you can have a "built-in" admin account.

talisunep
Posts: 140
Posted 18:21 May 30, 2013 |

im trying to get current user id to pass it to add review table in my addReview controller

User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
int userId = user.getId();

but when i run program my controller for addReview (METHOD POST)  throws an error saying that i cant invoke a User object under this controller as my @Model Attribute is Review review

so what im trying now is using

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String uname = auth.getName(); //get logged in


to get the username of the current logged in user via spring security and try to write a query from the controller to get the user id.
I tried using DAO but i think this returns objects only and like the above problem i cant pass User object under the addReview controller since ..
@Model Attribute is Review review

i also tried using
Integer userId = entityManager.createQuery( " id from User where userName = " + uname, User.class ).getFirstResult();-
-> but i get erro

is there any way to write a query from the controller to to get the current logged in user ID? i dont know if i am trying the right approach?

 

 

Last edited by talisunep at 18:22 May 30, 2013.
cysun
Posts: 2935
Posted 20:14 May 30, 2013 |
talisunep wrote:

im trying to get current user id to pass it to add review table in my addReview controller

User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
int userId = user.getId();

but when i run program my controller for addReview (METHOD POST)  throws an error saying that i cant invoke a User object under this controller as my @Model Attribute is Review review

so what im trying now is using

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String uname = auth.getName(); //get logged in


to get the username of the current logged in user via spring security and try to write a query from the controller to get the user id.
I tried using DAO but i think this returns objects only and like the above problem i cant pass User object under the addReview controller since ..
@Model Attribute is Review review

i also tried using
Integer userId = entityManager.createQuery( " id from User where userName = " + uname, User.class ).getFirstResult();-
-> but i get erro

is there any way to write a query from the controller to to get the current logged in user ID? i dont know if i am trying the right approach?

There's nothing wrong with using DAO to get a User object using username. The error you got must be because of something else.
 

talisunep
Posts: 140
Posted 20:30 May 30, 2013 |

im trying to user userDao now

User user = userDao.getId(uname);

getting error

org.hibernate.exception.SQLGrammarException: could not extract ResultSet

my dao implementation is

@Override
public User getId(String uname) {
User user  = entityManager.createQuery("from User where userName="+uname, User.class).getSingleResult();
return user;

}

do you think .getSingleResult will return the user object?

talisunep
Posts: 140
Posted 21:51 May 30, 2013 |

sorry my query was not correct ..its working now with
 

User user  = entityManager.createQuery("from User where userName= :uname", User.class).setParameter("uname", uname).getSingleResult();