我创建了这个类UserPrincipal来获取我的自定义Hibernate User类的用户ID。
public class UserPrincipal extends org.springframework.security.core.userdetails.User {
public User getUser() {
return user;
}
public UserPrincipal(String username, String password, boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities, User user) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.user = user;
}
private final User user;
}
然而,当我像这样使用它时:
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = ((UserPrincipal) principal).getUser();
我收到以下错误:
java.lang.ClassCastException:org.springframework.security.core.userdetails.User无法强制转换为UserPrincipal
我认为我实现构造函数的方式有误,但我不确定。
或者它与我登录用户的方式不匹配有什么关系?我正在使用Spring Security。
答案 0 :(得分:0)
CREATE PROCEDURE SPTEST1(IN tr_code INT, IN pen_type INT, IN pen_code INT, IN due_code INT, IN ord_dt DATE, IN finyr_cd INT, IN user_id varchar(50), IN server_ip varchar(100), OUT res INTEGER)
SPECIFIC sptest1
LANGUAGE SQL
P1:BEGIN
DECLARE trcd INTEGER;
DECLARE pentyp INTEGER;
DECLARE pencd INTEGER;
DECLARE duecd INTEGER;
DECLARE orddt DATE;
DECLARE finyrcd INTEGER;
DECLARE userid VARCHAR(50);
DECLARE serverip VARCHAR(100);
DECLARE ls_pentype CHAR(1);
DECLARE ls_voted CHAR(1);
DECLARE li_crmonth INTEGER;
DECLARE li_cryear INTEGER;
DECLARE ls_adhoc CHAR(1);
DECLARE ld_elgdt DATE;
DECLARE li_duecode INTEGER;
DECLARE li_headid INTEGER;
DECLARE tr_whr varchar(500);
DECLARE code_whr varchar(500);
DECLARE sqlstmt varchar(1000);
DECLARE EOF int DEFAULT 0;
DECLARE v_duplicate INT DEFAULT 0;--
DECLARE c_duplicate CONDITION FOR SQLSTATE '23505';
DECLARE STMT VARCHAR(120);
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
SET trcd = tr_code;
SET pentyp = pen_type;
SET pencd = pen_code;
SET duecd = due_code;
SET orddt = ord_dt;
SET finyrcd = finyr_cd;
SET userid = user_id;
SET serverip = server_ip;
SET ls_voted = 'V';
IF duecd > 50 THEN
SET ls_voted = 'C';
END IF;
FOR v_row AS <select query>
DO
SET ls_pentype = v_row.fmly_serv_oth_pen;
END FOR;
FOR date_month AS <select query>
DO
SET li_crmonth = date_month.credit_month;
SET li_cryear = date_month.credit_year;
END FOR;
FOR adhoc_row AS <select query>
DO
SET ls_adhoc = adhoc_row.is_adhoc_due;
END FOR;
FOR elgdt_row AS <select query>
DO
SET ld_elgdt = elgdt_row.eligible_uptodt;
END FOR;
FOR head_row AS <select query>
DO
SET li_headid = head_row.head_id;
SET li_duecode = duecd;
END FOR;
--portion for delete. have to enter
P2:BEGIN
DECLARE TRPOC CURSOR FOR <select query>
OPEN TRPOC;
FETCH FROM TRPOC INTO trcd;
WHILE(SQLSTATE = '00000') DO
insert into bg_run_test values(char(trcd));
END WHILE;
CLOSE TRPOC;
END P2;
END P1@
答案 1 :(得分:0)
而不是从spring-security扩展用户类,你最好实现Spring安全性给出的UserDetails接口。请阅读以下代码。为项目提供自定义userDetailsService实现。我希望这会对你有帮助 - http://docs.spring.io/spring-security/site/docs/3.0.x/reference/technical-overview.html#d0e1613
public class LoggedUser implements UserDetails{
private User user;
// setter and getter of user
public LoggedUser (User user){
this.user=user;
}
public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> authorities=new ArrayList<SimpleGrantedAuthority>();
for (String role : user.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
public String getPassword() {
return user.getPassword();
}
public String getUsername() {
// TODO Auto-generated method stub
return user.getUsername();
}
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}
现在使用以下代码获取User对象:
LoggedUser principal = (LoggedUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = pricipal.getUser();
答案 2 :(得分:-1)
只需检查您使用安全用户数据库登录的用户即可。