Project

General

Profile

« Previous | Next » 

Revision 19c39bf4

Added by Cherian Mathew over 8 years ago

#5029 Implement oneclick login for remoting

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/LoginManager.java
58 58
	 */
59 59
	public boolean authenticate(String username, String password){
60 60

  
61
		// close all open editors
62
		if(!AbstractUtility.closeAll()){
63
			return false;
64
		}
61
	    // close all open editors
62
	    if(!AbstractUtility.closeAll()){
63
	        return false;
64
	    }
65

  
66

  
67
	    try{
68
	        doAuthenticate(username, password);
69
	    } catch (CdmAuthenticationException e) {
70
	        MessagingUtils.warningDialog("Could not authenticate", this, e.getMessage());
71
        }
72
	    return false;
73
	}
65 74

  
75
	public void doAuthenticate(String username, String password) throws CdmAuthenticationException {
76
	    try {
77
	        SecurityContextHolder.clearContext();
78
	        Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
79

  
80
	        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
81
	        Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
82

  
83
	        User user = (User) authentication.getPrincipal();
84
	        /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
85
	         * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
86
	        user.initAuthorities();
87

  
88
	        if(logger.isDebugEnabled()){
89
	            StringBuilder gaText = new StringBuilder();
90
	            String indent = "    ";
91
	            Set<GrantedAuthority> gaSet = user.getGrantedAuthorities();
92
	            _logGrantedAuthotities(gaText, indent, gaSet);
93
	            for(Group gr : user.getGroups()){
94
	                gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
95
	                _logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
96
	            }
97
	            logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
98
	        }
99

  
100
	        authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
101
	        SecurityContextHolder.getContext().setAuthentication(authentication);
102
	        CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
103

  
104
	        if(!authentication.equals(lastAuthentication)){
105
	            this.setChanged();
106
	            this.notifyObservers();
107
	        }
108
	    } catch(BadCredentialsException e){
109
	        throw new CdmAuthenticationException("Login and/or Password incorrect", e);
110
	    } catch(LockedException e){
111
	        throw new CdmAuthenticationException("Account is locked", e);
112
	    } catch(IllegalArgumentException e){
113
	        throw new CdmAuthenticationException("Login and/or Password empty", e);
114
	    }
66 115

  
67
		try{
68
			SecurityContextHolder.clearContext();
69
			Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
70

  
71
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
72
			Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
73

  
74
			User user = (User) authentication.getPrincipal();
75
			/* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
76
			 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
77
			user.initAuthorities();
78

  
79
			if(logger.isDebugEnabled()){
80
				StringBuilder gaText = new StringBuilder();
81
				String indent = "    ";
82
				Set<GrantedAuthority> gaSet = user.getGrantedAuthorities();
83
				_logGrantedAuthotities(gaText, indent, gaSet);
84
				for(Group gr : user.getGroups()){
85
					gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
86
					_logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
87
				}
88
				logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
89
			}
90

  
91
			authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
92
			SecurityContextHolder.getContext().setAuthentication(authentication);
93
			CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
94

  
95
			if(!authentication.equals(lastAuthentication)){
96
				this.setChanged();
97
				this.notifyObservers();
98
			}
99
			return true;
100
		}
101
		catch(BadCredentialsException e){
102
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
103
		}
104
		catch(LockedException e){
105
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
106
		}
107
		catch(IllegalArgumentException e){
108
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
109
		}
110
		return false;
111 116
	}
112 117

  
113 118
	private void _logGrantedAuthotities(StringBuilder gaText, String indent,

Also available in: Unified diff