// 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);
// }


import com.lixiaocong.cms.entity.User;
import com.lixiaocong.cms.repository.IUserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
/*
  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;



//TODO if don't set order, there will be an error, to be fixed
@Profile("develop")
@Order(1)
@Configuration
public class DevWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private Log log = LogFactory.getLog(getClass());

    private IUserRepository userRepository;

    @Autowired
    public DevWebSecurityConfig(IUserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll().and().formLogin().loginPage("/signin").defaultSuccessUrl("/blog").and().logout().logoutUrl("/logout").logoutSuccessUrl("/blog").and().rememberMe().rememberMeParameter("remember-me").and().csrf().disable();
// 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/dev/DevWebSecurityConfig.java
import com.lixiaocong.cms.entity.User;
import com.lixiaocong.cms.repository.IUserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.context.SecurityContextPersistenceFilter;

/*
  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;



//TODO if don't set order, there will be an error, to be fixed
@Profile("develop")
@Order(1)
@Configuration
public class DevWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private Log log = LogFactory.getLog(getClass());

    private IUserRepository userRepository;

    @Autowired
    public DevWebSecurityConfig(IUserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll().and().formLogin().loginPage("/signin").defaultSuccessUrl("/blog").and().logout().logoutUrl("/logout").logoutSuccessUrl("/blog").and().rememberMe().rememberMeParameter("remember-me").and().csrf().disable();
        User admin = userRepository.findByUsername("admin");