Announcement Announcement Module
Collapse
No announcement yet.
StackTrace instead of errorPage Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • StackTrace instead of errorPage

    Hi,

    this is my code where I check if a user exists in the database. When I write a username correct and password wrong I get a page with an error description, as it is configured in spring-security.xml. But when there is no user in the database and an exception below is triggered I get an error page with a stack trace. How can I catch an exception, so it will display as it should (in a notice div on the login page).


    PHP Code:
    @Service("userDetailsService")
    public class 
    UserDetailsServiceImpl implements UserDetailsService {
        
        private static 
    Log logger LogFactory.getLog(UserDetailsServiceImpl.class);
        
        @
    Autowired
        
    private UserDetailsDao userDetailsDao;
        
        @
    Override
        
    @Transactional
        
    public UserDetails loadUserByUsername(String username)
                
    throws UsernameNotFoundExceptionDataAccessException {
            if (
    logger.isDebugEnabled()) {
                
    logger.debug("UserDetailsServiceImpl::getSystemUSer");
            }
            
            
    SystemUser systemUser this.userDetailsDao.findByUsername(username);
            if (
    systemUser != null) {
                return 
    systemUser;
            }
            throw new 
    AccessDeniedException("Access denied.");
        }
            

    Any help appreciated.

  • #2
    As indicated in the javadoc of UserDetailsService your UserDetailsServiceImpl should be throwing a UsernameNotFoundException if the user is not found not an AccessDeniedException

    Comment


    • #3
      Thanks that was it. Anyway, I'm throwing now an throw new UsernameNotFoundException("Access denied") but on the fron form I get Bad credentials message. Do I have to set something to allow my custom message?

      Comment


      • #4
        You can use the SPRING_SECURITY_LAST_EXCEPTION session attribute to get this exception. You can then use the message on that exception to display a custom error message. Refer to the contacts sample application to see a JSP that does this.

        Comment

        Working...
        X