模板修改
This commit is contained in:
@@ -20,38 +20,43 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
* @author Administrator
|
||||
*/
|
||||
public class MyUserDetailService implements UserDetailsService {
|
||||
|
||||
|
||||
private final UserHelper userHelper;
|
||||
|
||||
|
||||
public MyUserDetailService(UserHelper u) {
|
||||
this.userHelper = u;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
|
||||
|
||||
|
||||
Tbl_Security_User user = userHelper.getTbl_Security_User(userName);
|
||||
|
||||
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("用户不存在!");
|
||||
}
|
||||
|
||||
|
||||
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_" + user.getRole()));
|
||||
boolean enabled = true;
|
||||
boolean accountNonExpired = true;
|
||||
boolean credentialsNonExpired = true;
|
||||
boolean accountNonLocked = true;
|
||||
|
||||
|
||||
UserInfo userdetails = new UserInfo();
|
||||
userdetails.setId(user.getId());
|
||||
userdetails.setUsername(user.getUsername());
|
||||
userdetails.setPassword(user.getPassword());
|
||||
userdetails.setEmail(user.getEmail());
|
||||
userdetails.setRole(user.getRole());
|
||||
userdetails.setRoleName(user.getRolename());
|
||||
userdetails.setCode(user.getCode());
|
||||
userdetails.setEnabled(enabled);
|
||||
userdetails.setAccountNonLocked(accountNonLocked);
|
||||
userdetails.setAccountNonExpired(accountNonExpired);
|
||||
userdetails.setCredentialsNonExpired(credentialsNonExpired);
|
||||
return userdetails;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,13 +7,17 @@ package com.ifish.config;
|
||||
|
||||
import com.ifish.helper.UserHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.config.BeanIds;
|
||||
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.web.filter.CharacterEncodingFilter;
|
||||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -36,20 +40,21 @@ public class SecuredConfig extends WebSecurityConfigurerAdapter {
|
||||
//http.addFilterBefore(encodingFilter, CsrfFilter.class).csrf().requireCsrfProtectionMatcher(mycsrfRequestMatcher());
|
||||
|
||||
//取消csrf验证
|
||||
//http.csrf().disable();
|
||||
http.csrf().disable();
|
||||
//免登陆
|
||||
//http.rememberMe().rememberMeServices(rememberMeServinces());
|
||||
http.authorizeRequests() //对下面几个url进行授权
|
||||
//.antMatchers("/news/*","/school/*").hasAnyRole("1","4") //UserBean的utype为1或4才能访问'/news/*','/school/*'
|
||||
//.antMatchers("/score/*").hasRole("4") //同上,utype为4的才能访问
|
||||
//.antMatchers("/**").authenticated() //authenticated()只有登陆验证过用户才可以访问hasRole()只有特定权限,这里是Utype用户才可以访问
|
||||
.antMatchers("/index").authenticated()
|
||||
.antMatchers("/").authenticated()
|
||||
.antMatchers("/admin/**").hasAnyRole("4")
|
||||
.and() //相当于方法结束符,and结束的几个方法是并行的,没有顺序之分,最后一个方法不需要and结束
|
||||
.formLogin()//配置FBA。登陆,若有role权限,formLogin不能少
|
||||
.loginProcessingUrl("/login")//登陆后是否跳转前一个URL,参数为登陆的POST地址
|
||||
.loginPage("/") //登陆页面
|
||||
.loginPage("/login") //登陆页面
|
||||
.defaultSuccessUrl("/", false)
|
||||
.failureUrl("/")
|
||||
.failureUrl("/login")
|
||||
//.failureUrl("/login?error") //登陆失败跳转页面
|
||||
.usernameParameter("username")
|
||||
.passwordParameter("password")
|
||||
@@ -75,4 +80,10 @@ public class SecuredConfig extends WebSecurityConfigurerAdapter {
|
||||
// .withUser("3").password("3").roles("ADMIN");
|
||||
|
||||
}
|
||||
|
||||
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
|
||||
@Override
|
||||
public AuthenticationManager authenticationManagerBean() throws Exception {
|
||||
return super.authenticationManagerBean();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ public class UserInfo implements UserDetails, CredentialsContainer {
|
||||
|
||||
@Override
|
||||
public void eraseCredentials() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
password = null;
|
||||
}
|
||||
|
||||
private static class AuthorityComparator implements
|
||||
|
||||
@@ -6,15 +6,21 @@
|
||||
package com.ifish.config;
|
||||
|
||||
import com.ifish.util.IfishFilePath;
|
||||
import java.io.IOException;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -36,23 +42,51 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
// @Bean
|
||||
// public ViewResolver viewResolver() {
|
||||
// InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
// resolver.setPrefix("/WEB-INF/views/");
|
||||
// resolver.setSuffix(".jsp");
|
||||
// resolver.setExposeContextBeansAsAttributes(true);
|
||||
// return resolver;
|
||||
// }
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable(); //配置静态文件处理
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
// TODO Auto-generated method stub
|
||||
super.addResourceHandlers(registry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//registry.addInterceptor(myInterceptorAdapter());
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
|
||||
// Tiles
|
||||
@Bean
|
||||
public TilesConfigurer tilesConfigurer() {
|
||||
TilesConfigurer tiles = new TilesConfigurer();
|
||||
tiles.setDefinitions(new String[]{"/WEB-INF/layout/tiles.xml"});
|
||||
tiles.setCheckRefresh(true);
|
||||
return tiles;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
resolver.setPrefix("/WEB-INF/views/");
|
||||
resolver.setSuffix(".jsp");
|
||||
resolver.setExposeContextBeansAsAttributes(true);
|
||||
return resolver;
|
||||
return new TilesViewResolver();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置静态资源的处理
|
||||
*
|
||||
* @param configurer
|
||||
*/
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();;
|
||||
@Bean
|
||||
public MultipartResolver multipartResolver() throws IOException {
|
||||
return new StandardServletMultipartResolver();
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public MyInterceptorAdapter myInterceptorAdapter() {
|
||||
// return new MyInterceptorAdapter();
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.ifish.controller;
|
||||
|
||||
import java.util.Calendar;
|
||||
import com.ifish.helper.SecurityService;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.jsp.jstl.core.Config;
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
@@ -21,6 +22,9 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
@Controller
|
||||
public class Index {
|
||||
|
||||
@Autowired
|
||||
private SecurityService securityService;
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
||||
public ModelAndView login(
|
||||
@RequestParam(value = "error", required = false) String error,
|
||||
@@ -44,7 +48,7 @@ public class Index {
|
||||
return mv;
|
||||
}
|
||||
}
|
||||
mv.setViewName("index");
|
||||
mv.setViewName("login");
|
||||
return mv;
|
||||
|
||||
}
|
||||
@@ -56,4 +60,11 @@ public class Index {
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping(value = {"", "/", "index"}, method = RequestMethod.GET)
|
||||
public ModelAndView test1() {
|
||||
ModelAndView mv = new ModelAndView();
|
||||
mv.setViewName("index");
|
||||
return mv;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.ifish.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Controller
|
||||
public class User {
|
||||
|
||||
@RequestMapping(value = {"/userlist"}, method = RequestMethod.GET)
|
||||
public ModelAndView getUserList() {
|
||||
ModelAndView mv = new ModelAndView();
|
||||
mv.setViewName("userlist");
|
||||
return mv;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.ifish.helper;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface SecurityService {
|
||||
|
||||
public void autologinrequest(String username, String password, HttpServletRequest request);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.ifish.helper;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Component
|
||||
public class SecurityServiceI implements SecurityService {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@Override
|
||||
public void autologinrequest(String username, String password, HttpServletRequest request) {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
||||
//RememberMeAuthenticationToken tooken = new RememberMeAuthenticationToken();
|
||||
token.setDetails(new WebAuthenticationDetails(request));
|
||||
Authentication authenticatedUser = authenticationManager.authenticate(token);
|
||||
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user