// Path: src/main/java/com/lixiaocong/cms/entity/User.java
// @Entity(name = "User")
// public class User extends AbstractEntity {
//     @Column(nullable = false, unique = true)
//     private String username;
// 
//     @JsonIgnore
//     @Column(nullable = false)
//     private String password;
// 
//     @Column(nullable = false)
//     private boolean admin;
// 
//     @JsonIgnore
//     @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
//     private Set<Article> articles;
// 
//     @JsonIgnore
//     @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
//     private Set<Comment> comments;
// 
//     public User() {
//         admin = false;
//         articles = new HashSet<>();
//         comments = new HashSet<>();
//     }
// 
//     public User(String username, String password) {
//         admin = false;
//         articles = new HashSet<>();
//         comments = new HashSet<>();
// 
//         this.username = username;
//         this.password = password;
//     }
// 
//     public Set<Comment> getComments() {
//         return comments;
//     }
// 
//     public void setComments(Set<Comment> comments) {
//         this.comments = comments;
//     }
// 
//     public Set<Article> getArticles() {
//         return articles;
//     }
// 
//     public void setArticles(Set<Article> articles) {
//         this.articles = articles;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public boolean isAdmin() {
//         return admin;
//     }
// 
//     public void setAdmin(boolean admin) {
//         this.admin = admin;
//     }
// }
// 
// Path: src/main/java/com/lixiaocong/cms/repository/IUserRepository.java
// public interface IUserRepository extends JpaRepository<User, Long> {
//     User findByUsername(String username);
// }
// 
// Path: src/main/java/com/lixiaocong/cms/security/DaoBasedUserDetails.java
// public class DaoBasedUserDetails extends User {
//     private long id;
// 
//     public DaoBasedUserDetails(long id, String username, String password, Collection<? extends GrantedAuthority> authorities) {
//         super(username, password, authorities);
//         this.id = id;
//     }
// 
//     public long getId() {
//         return id;
//     }
// 
//     public void setId(long id) {
//         this.id = id;
//     }
// 
//     @Override
//     public String toString() {
//         return "User " + id;
//     }
// }


import com.lixiaocong.cms.entity.User;
import com.lixiaocong.cms.repository.IUserRepository;
import com.lixiaocong.cms.security.DaoBasedUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import javax.servlet.*;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
/*
  BSD 3-Clause License

  Copyright (c) 2016, lixiaocong(lxccs@iCloud.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.lixiaocong.cms.dev;



public class DevSigninFilter implements Filter {
    private final IUserRepository userRepository;

    DevSigninFilter(IUserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

// Path: src/main/java/com/lixiaocong/cms/entity/User.java
// @Entity(name = "User")
// public class User extends AbstractEntity {
//     @Column(nullable = false, unique = true)
//     private String username;
// 
//     @JsonIgnore
//     @Column(nullable = false)
//     private String password;
// 
//     @Column(nullable = false)
//     private boolean admin;
// 
//     @JsonIgnore
//     @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
//     private Set<Article> articles;
// 
//     @JsonIgnore
//     @OneToMany(mappedBy = "user", cascade = {CascadeType.REMOVE})
//     private Set<Comment> comments;
// 
//     public User() {
//         admin = false;
//         articles = new HashSet<>();
//         comments = new HashSet<>();
//     }
// 
//     public User(String username, String password) {
//         admin = false;
//         articles = new HashSet<>();
//         comments = new HashSet<>();
// 
//         this.username = username;
//         this.password = password;
//     }
// 
//     public Set<Comment> getComments() {
//         return comments;
//     }
// 
//     public void setComments(Set<Comment> comments) {
//         this.comments = comments;
//     }
// 
//     public Set<Article> getArticles() {
//         return articles;
//     }
// 
//     public void setArticles(Set<Article> articles) {
//         this.articles = articles;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public boolean isAdmin() {
//         return admin;
//     }
// 
//     public void setAdmin(boolean admin) {
//         this.admin = admin;
//     }
// }
// 
// Path: src/main/java/com/lixiaocong/cms/repository/IUserRepository.java
// public interface IUserRepository extends JpaRepository<User, Long> {
//     User findByUsername(String username);
// }
// 
// Path: src/main/java/com/lixiaocong/cms/security/DaoBasedUserDetails.java
// public class DaoBasedUserDetails extends User {
//     private long id;
// 
//     public DaoBasedUserDetails(long id, String username, String password, Collection<? extends GrantedAuthority> authorities) {
//         super(username, password, authorities);
//         this.id = id;
//     }
// 
//     public long getId() {
//         return id;
//     }
// 
//     public void setId(long id) {
//         this.id = id;
//     }
// 
//     @Override
//     public String toString() {
//         return "User " + id;
//     }
// }


// Path: src/main/java/com/lixiaocong/cms/dev/DevSigninFilter.java
import com.lixiaocong.cms.entity.User;
import com.lixiaocong.cms.repository.IUserRepository;
import com.lixiaocong.cms.security.DaoBasedUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import javax.servlet.*;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/*
  BSD 3-Clause License

  Copyright (c) 2016, lixiaocong(lxccs@iCloud.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.lixiaocong.cms.dev;



public class DevSigninFilter implements Filter {
    private final IUserRepository userRepository;

    DevSigninFilter(IUserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

        User admin = userRepository.findByUsername("admin");