Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cm.sb.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.cm.sb.model.SysUser;
- import com.cm.sb.repository.SysUserRepository;
- @Service
- public class SysUserService {
- @Autowired
- SysUserRepository sysUserRepository;
- public SysUser findByusername(String username) {
- return sysUserRepository.findByUsername(username);
- }
- }
- package com.cm.sb;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- @SpringBootApplication(scanBasePackages={"com.cm.sb"},exclude = { SecurityAutoConfiguration.class })
- @EnableJpaRepositories(basePackages = "com.cm.sb")
- public class SbApplication extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(SbApplication.class);
- }
- public static void main(String[] args) {
- SpringApplication.run(SbApplication.class, args);
- }
- }
- spring.datasource.url=jdbc:mysql://localhost:3306/sb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=
- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
- spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
- spring.jpa.show-sql=false
- spring.jpa.hibernate.ddl-auto=none
- spring.mvc.view.prefix: /WEB-INF/jsp/
- spring.mvc.view.suffix: .jsp
- security.user.password=ciro
- security.user.username=ciro
- package com.cm.sb.config;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- 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.builders.WebSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.provisioning.InMemoryUserDetailsManager;
- import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
- @Configuration
- @EnableWebSecurity
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
- @Resource(name = "userDetailService")
- private UserDetailsService userDetailsService;
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("**/login"))
- .and().authorizeRequests().antMatchers("/dashboard").hasRole("USER")
- .and().authorizeRequests().antMatchers("/home").permitAll()
- .and().formLogin().defaultSuccessUrl("/dashboard").loginPage("/login")
- .and().logout().permitAll();
- }
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userDetailsService);
- }
- @Override
- public void configure(WebSecurity web) throws Exception {
- web.ignoring().antMatchers("/*.css");
- web.ignoring().antMatchers("/*.js");
- }
- }
- package com.cm.sb.repository;
- import com.cm.sb.model.SysUser;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- @Repository("sysUserRepository")
- public interface SysUserRepository extends JpaRepository<SysUser, Integer> {
- SysUser findByUsername(String username);
- }
Add Comment
Please, Sign In to add comment