javax.servlet.ServletException: javax.servlet.jsp.JspException: org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.userid' of bean class

KimHyunKi·2020년 6월 26일
0

Error

목록 보기
2/5

로그인 했을 때 사용자의 이름을 보여주기 위해

<sec:authentication property="principal.userid"/>님, 반갑습니다. 

부분을 추가했는데 에러가 발생했다..
찾아보니 여러 해결법이 있었는데 내가 해결한 방법을 공유하려한다.

해결법

1. 우선 위에서 principal은 Authentication.getPrincipal() 으로 얻을 수 있는데 return되는 객체가 2개가 있다.

1) 인증되지 않은 상태를 return하는 String
2) 인증에 성공한 정보가 담긴 Object객체

그래서 성공했을때 실행되는 함수인 UsernamePasswordAuthenticationToken함수를 봤다.

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
			Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		this.credentials = credentials;
		super.setAuthenticated(true); // must use super, as we override
	}

Object객체를 받아야하는데 내가 커스터마이징한 함수에선

@Override
	public org.springframework.security.core.Authentication authenticate(org.springframework.security.core.Authentication authentication) throws AuthenticationException {

		String userid = (String)authentication.getPrincipal();
		String userpw = (String)authentication.getCredentials();
		LoginVO Lvo = (LoginVO)service.loadUserByUsername(userid);

		logger.info("Lvo = "+Lvo);
		if(!matchPassword(userpw,Lvo.getPassword())) {
			logger.info("=====비밀번호가 불일치=====");
			throw new BadCredentialsException(userid);
		}
		
		Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>)Lvo.getAuthorities();
		
		if(!Lvo.isEnabled()) {
			logger.info("=====계정 활성화 여부=====");
			throw new BadCredentialsException(userid);
		}
		
		return new UsernamePasswordAuthenticationToken(userid,userpw,authorities);
	}

String을 인자로 전달했다.

그래서 VO객체를 전달해주었다.

수정 전
UsernamePasswordAuthenticationToken(userid,userpw,authorities);

수정 후
UsernamePasswordAuthenticationToken(Lvo,Lvo,authorities);

2. <sec:authentication property="" 에서 얻을 수 있는 정보는 username,password,email 등이 있다. userid는 얻을 수 없으므로 username으로 변경

수정 전
<sec:authentication property="principal.id"/>님, 반갑습니다.

수정 후
<sec:authentication property="principal.username"/>${username }님, 반갑습니다.

profile
Developer

0개의 댓글