// Path: lolibox-server/src/main/java/io/loli/box/dao/JpaReMemberMeRepository.java
// public interface JpaReMemberMeRepository extends JpaRepository<PersistentLogins, String>, PersistentTokenRepository {
// 
//     @Modifying
//     @Transactional
//     default void createNewToken(PersistentRememberMeToken token) {
//         PersistentLogins logins = new PersistentLogins();
//         logins.setLastUsed(token.getDate());
//         logins.setSeries(token.getSeries());
//         logins.setToken(token.getTokenValue());
//         logins.setUsername(token.getUsername());
//         this.save(logins);
//     }
// 
//     @Modifying
//     @Transactional
//     default void updateToken(String series, String tokenValue, Date lastUsed) {
//         updateTokenAndLastUsedBySeries(tokenValue, lastUsed, series);
//     }
// 
//     @Query("update PersistentLogins set token=?, lastUsed=? where series=?")
//     @Modifying
//     public int updateTokenAndLastUsedBySeries(String token, Date lastUsed, String series);
// 
//     public List<PersistentLogins> findBySeries(String series);
// 
//     default PersistentRememberMeToken getTokenForSeries(String seriesId) {
//         List<PersistentLogins> logins = this.findBySeries(seriesId);
//         if (logins.isEmpty()) {
//             return null;
//         }
//         PersistentLogins login = logins.get(0);
//         return new PersistentRememberMeToken(login.getUsername(), login.getSeries(), login.getToken(), login.getLastUsed());
//     }
// 
//     @Modifying
//     public int deleteByUsername(String username);
// 
//     @Transactional
//     default void removeUserTokens(String username){
//         this.deleteByUsername(username);
//     }
// }
// 
// Path: lolibox-server/src/main/java/io/loli/box/entity/Role.java
// public enum Role {
//     ROLE_USER("ROLE_USER"), ADMIN("ROLE_ADMIN");
// 
//     private String name;
// 
// 
//     Role(String name) {
//         this.name = name;
//     }
// 
//     @Override
//     public String toString() {
//         return name;
//     }
// }
// 
// Path: lolibox-server/src/main/java/io/loli/box/social/RepositoryUserDetailsService.java
// @Component
// public class RepositoryUserDetailsService implements UserDetailsService {
// 
//     private UserRepository repository;
// 
//     @Autowired
//     public RepositoryUserDetailsService(UserRepository repository) {
//         this.repository = repository;
//     }
// 
//     @Autowired
//     private AdminProperties adminProperties;
// 
//     @Override
//     public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
//         User user = repository.findByEmail(email);
//         if (user == null) {
//             user = repository.findByUserName(email);
//         }
// 
//         if (user == null) {
//             throw new UsernameNotFoundException("No user found with username: " + email);
//         }
// 
//         SocialUserDetails.Builder builder = SocialUserDetails.getBuilder()
//                 .id(user.getId())
//                 .password(user.getPassword())
//                 .role(user.getRole());
//         if (adminProperties.getEmail() != null && adminProperties.getEmail().equals(user.getEmail())) {
//             builder = builder.role(Role.ADMIN);
//         }
// 
// 
//         SocialUserDetails principal = builder.socialSignInProvider(user.getSignInProvider())
//                 .username(user.getUserName())
//                 .email(user.getEmail())
//                 .build();
// 
//         return principal;
//     }
// }


import io.loli.box.dao.JpaReMemberMeRepository;
import io.loli.box.entity.Role;
import io.loli.box.social.RepositoryUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
package io.loli.box;


/**
 * @author choco
 */
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(6)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();
// Path: lolibox-server/src/main/java/io/loli/box/dao/JpaReMemberMeRepository.java
// public interface JpaReMemberMeRepository extends JpaRepository<PersistentLogins, String>, PersistentTokenRepository {
// 
//     @Modifying
//     @Transactional
//     default void createNewToken(PersistentRememberMeToken token) {
//         PersistentLogins logins = new PersistentLogins();
//         logins.setLastUsed(token.getDate());
//         logins.setSeries(token.getSeries());
//         logins.setToken(token.getTokenValue());
//         logins.setUsername(token.getUsername());
//         this.save(logins);
//     }
// 
//     @Modifying
//     @Transactional
//     default void updateToken(String series, String tokenValue, Date lastUsed) {
//         updateTokenAndLastUsedBySeries(tokenValue, lastUsed, series);
//     }
// 
//     @Query("update PersistentLogins set token=?, lastUsed=? where series=?")
//     @Modifying
//     public int updateTokenAndLastUsedBySeries(String token, Date lastUsed, String series);
// 
//     public List<PersistentLogins> findBySeries(String series);
// 
//     default PersistentRememberMeToken getTokenForSeries(String seriesId) {
//         List<PersistentLogins> logins = this.findBySeries(seriesId);
//         if (logins.isEmpty()) {
//             return null;
//         }
//         PersistentLogins login = logins.get(0);
//         return new PersistentRememberMeToken(login.getUsername(), login.getSeries(), login.getToken(), login.getLastUsed());
//     }
// 
//     @Modifying
//     public int deleteByUsername(String username);
// 
//     @Transactional
//     default void removeUserTokens(String username){
//         this.deleteByUsername(username);
//     }
// }
// 
// Path: lolibox-server/src/main/java/io/loli/box/entity/Role.java
// public enum Role {
//     ROLE_USER("ROLE_USER"), ADMIN("ROLE_ADMIN");
// 
//     private String name;
// 
// 
//     Role(String name) {
//         this.name = name;
//     }
// 
//     @Override
//     public String toString() {
//         return name;
//     }
// }
// 
// Path: lolibox-server/src/main/java/io/loli/box/social/RepositoryUserDetailsService.java
// @Component
// public class RepositoryUserDetailsService implements UserDetailsService {
// 
//     private UserRepository repository;
// 
//     @Autowired
//     public RepositoryUserDetailsService(UserRepository repository) {
//         this.repository = repository;
//     }
// 
//     @Autowired
//     private AdminProperties adminProperties;
// 
//     @Override
//     public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
//         User user = repository.findByEmail(email);
//         if (user == null) {
//             user = repository.findByUserName(email);
//         }
// 
//         if (user == null) {
//             throw new UsernameNotFoundException("No user found with username: " + email);
//         }
// 
//         SocialUserDetails.Builder builder = SocialUserDetails.getBuilder()
//                 .id(user.getId())
//                 .password(user.getPassword())
//                 .role(user.getRole());
//         if (adminProperties.getEmail() != null && adminProperties.getEmail().equals(user.getEmail())) {
//             builder = builder.role(Role.ADMIN);
//         }
// 
// 
//         SocialUserDetails principal = builder.socialSignInProvider(user.getSignInProvider())
//                 .username(user.getUserName())
//                 .email(user.getEmail())
//                 .build();
// 
//         return principal;
//     }
// }


// Path: lolibox-server/src/main/java/io/loli/box/SecurityConfig.java
import io.loli.box.dao.JpaReMemberMeRepository;
import io.loli.box.entity.Role;
import io.loli.box.social.RepositoryUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

package io.loli.box;


/**
 * @author choco
 */
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(6)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();
        registry.antMatchers("/admin/**").hasAuthority(Role.ADMIN.toString())