Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.dimafeng.cards.model; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.PersistenceConstructor; | ||
import org.springframework.data.mongodb.core.index.CompoundIndex; | ||
import org.springframework.data.mongodb.core.index.CompoundIndexes; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken; | ||
|
||
import java.util.Date; | ||
|
||
@Document | ||
@CompoundIndexes({ | ||
@CompoundIndex(name = "i_username", def = "{'username': 1}"), | ||
@CompoundIndex(name = "i_series", def = "{'series': 1}") | ||
}) | ||
public class Token extends PersistentRememberMeToken { | ||
|
||
@Id | ||
private final String id; | ||
|
||
@PersistenceConstructor | ||
public Token(String id, String username, String series, String tokenValue, Date date) { | ||
super(username, series, tokenValue, date); | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.dimafeng.cards.repository; | ||
|
||
import com.dimafeng.cards.model.Token; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
public interface TokenRepository extends MongoRepository<Token, String> { | ||
Token findBySeries(String series); | ||
Token findByUsername(String username); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.dimafeng.cards.service; | ||
|
||
import com.dimafeng.cards.model.Token; | ||
import com.dimafeng.cards.repository.TokenRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken; | ||
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
|
||
@Component | ||
public class TokenService implements PersistentTokenRepository { | ||
|
||
@Autowired | ||
TokenRepository repository; | ||
|
||
@Override | ||
public void createNewToken(PersistentRememberMeToken token) { | ||
repository.save(new Token(null, | ||
token.getUsername(), | ||
token.getSeries(), | ||
token.getTokenValue(), | ||
token.getDate())); | ||
} | ||
|
||
@Override | ||
public void updateToken(String series, String tokenValue, Date lastUsed) { | ||
Token token = repository.findBySeries(series); | ||
repository.save(new Token(token.getId(), token.getUsername(), series, tokenValue, lastUsed)); | ||
} | ||
|
||
@Override | ||
public PersistentRememberMeToken getTokenForSeries(String seriesId) { | ||
return repository.findBySeries(seriesId); | ||
} | ||
|
||
@Override | ||
public void removeUserTokens(String username) { | ||
Token token = repository.findByUsername(username); | ||
repository.save(token); | ||
} | ||
|
||
|
||
} |