Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

java - How to add additional spring security field for login

I want to implement user login/signup using username, password and project (additional field). It is working with username and password but unable to implement to add additional field (project) since I am new to spring security.

  1. I am able to signup with email, username, password and project.
  2. I have added Project in UserPrincipal as shown in below code.

I need to implement custom authentication to add project during login but I am unable to figure out how to proceed further. Can anybody help please? i have already checked other solutions but did not figure out to implement.

question from:https://stackoverflow.com/questions/65617344/how-to-add-additional-spring-security-field-for-login

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understood correctly, you want to just add this project info to a User, during login/sign-up.

As @HarshVerma pointed out, in Spring it's only the login and the password you need to autheticate.

You could implement user's project as a JWT claim:

Jwts.builder()
     .setSubject(Long.toString(userPrincipal.getId()))
     .claim("project", myProject)
     .setIssuedAt(new Date())
     .setExpiration(expiryDate)
     .signWith(SignatureAlgorithm.HS512, jwtSecret)
     .compact();

and then retreive it to authorize accordingly:

Claims claims = new DefaultClaims();
    try{
        claims = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(jwt).getBody();
    } catch (SignatureException e){
        // signature exception
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...