ssm框架下实现SSO单点登录

版权声明:转载请标明出处,否则会依法追究其责任 https://blog.csdn.net/qq_41204714/article/details/84780955

1.定义对象并实现可序列化接口

public class Users implements Serializable {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
}

2.spring-dao配置redis相关文件

    <!--配置redis连接信息 -->
	<bean id="redisStandaloneConfiguration"
		class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
		<property name="hostName" value="192.168.80.21" />
		<property name="port" value="6379" />
		<property name="password" ref="redisPassword" />
	</bean>
	<!--配置redis连接工厂 -->
	<bean
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<constructor-arg name="standaloneConfig"
			ref="redisStandaloneConfiguration"></constructor-arg>
	</bean>
	<!--配置session的过滤器 -->
	<bean
		class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
		<!-- 过期时间10分钟 -->
		<property name="maxInactiveIntervalInSeconds" value="600"></property>
	</bean>

3.web.xml中设置session代理filter并加载配置文件

<!-- 增加spring session代理filter的配置 -->
	<filter>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
<!-- 上下文参数 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

4.Dao层和Service层业务逻辑(省略)

5.Controller层增加登录方法,设置session值

@RestController
public class UsersController {
	@Resource
	private UsersService usersServiceImpl;
	@RequestMapping("/show")
	public String Show(Model model) {
		model.addAttribute("list",usersServiceImpl.show());
		return "index.jsp";
	}
	@PostMapping("/login")
	public ReponseVo<Users> Login(@RequestBody Users user,HttpSession session){
		System.out.println(user.getUsername()+" "+user.getPassword());
		Users savesUser=usersServiceImpl.login(user);
        session.setAttribute("LoginUser",savesUser);
        return new ReponseVo<Users>(200,"success",savesUser) ;
	}
}

6.增加拦截器,若不登陆则不允许访问,跳转回登录页面

public class LoginIntegerceptor extends HandlerInterceptorAdapter{
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
	       Users loginUser= (Users) request.getSession().getAttribute("LoginUser");
	       if(loginUser==null){
	           ReponseVo<String> vo =new ReponseVo<>(403009, "没有登录,请刷新页面重新登录", null);

	           String json=new ObjectMapper().writeValueAsString(vo);
	           response.setContentType("application/json");
	           response.getWriter().println(json);
	           return false;
	       }else{
	           return true;
	       }
	}
}

 

展开阅读全文

没有更多推荐了,返回首页