1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.spring.ui; |
20 | |
|
21 | |
import java.awt.*; |
22 | |
import java.awt.event.ActionEvent; |
23 | |
import java.awt.event.ActionListener; |
24 | |
import java.awt.event.WindowAdapter; |
25 | |
import java.awt.event.WindowEvent; |
26 | |
import javax.swing.*; |
27 | |
|
28 | |
import org.springframework.beans.factory.InitializingBean; |
29 | |
import org.springframework.core.io.ClassPathResource; |
30 | |
|
31 | |
import org.apache.shiro.authz.AuthorizationException; |
32 | |
import org.apache.shiro.samples.spring.SampleManager; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class WebStartView implements ActionListener, InitializingBean { |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
private SampleManager sampleManager; |
53 | |
private JTextField valueField; |
54 | |
private JButton saveButton; |
55 | |
private JButton refreshButton; |
56 | |
private JButton secureMethod1Button; |
57 | |
private JButton secureMethod2Button; |
58 | |
private JButton secureMethod3Button; |
59 | |
private JFrame frame; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void setSampleManager(SampleManager sampleManager) { |
70 | 0 | this.sampleManager = sampleManager; |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void afterPropertiesSet() throws Exception { |
77 | 0 | ClassPathResource resource = new ClassPathResource("logo.png"); |
78 | 0 | ImageIcon icon = new ImageIcon(resource.getURL()); |
79 | 0 | JLabel logo = new JLabel(icon); |
80 | |
|
81 | 0 | valueField = new JTextField(20); |
82 | 0 | updateValueLabel(); |
83 | |
|
84 | 0 | saveButton = new JButton("Save Value"); |
85 | 0 | saveButton.addActionListener(this); |
86 | |
|
87 | 0 | refreshButton = new JButton("Refresh Value"); |
88 | 0 | refreshButton.addActionListener(this); |
89 | |
|
90 | 0 | JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
91 | 0 | valuePanel.add(valueField); |
92 | 0 | valuePanel.add(saveButton); |
93 | 0 | valuePanel.add(refreshButton); |
94 | |
|
95 | 0 | secureMethod1Button = new JButton("Method #1"); |
96 | 0 | secureMethod1Button.addActionListener(this); |
97 | |
|
98 | 0 | secureMethod2Button = new JButton("Method #2"); |
99 | 0 | secureMethod2Button.addActionListener(this); |
100 | |
|
101 | 0 | secureMethod3Button = new JButton("Method #3"); |
102 | 0 | secureMethod3Button.addActionListener(this); |
103 | |
|
104 | 0 | JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
105 | 0 | methodPanel.add(secureMethod1Button); |
106 | 0 | methodPanel.add(secureMethod2Button); |
107 | 0 | methodPanel.add(secureMethod3Button); |
108 | |
|
109 | 0 | frame = new JFrame("Apache Shiro Sample Application"); |
110 | 0 | frame.setSize(500, 200); |
111 | |
|
112 | 0 | Container panel = frame.getContentPane(); |
113 | 0 | panel.setLayout(new BorderLayout()); |
114 | 0 | panel.add(logo, BorderLayout.NORTH); |
115 | 0 | panel.add(valuePanel, BorderLayout.CENTER); |
116 | 0 | panel.add(methodPanel, BorderLayout.SOUTH); |
117 | |
|
118 | 0 | frame.setVisible(true); |
119 | 0 | frame.addWindowListener(new WindowAdapter() { |
120 | |
public void windowClosing(WindowEvent e) { |
121 | 0 | System.exit(0); |
122 | 0 | } |
123 | |
}); |
124 | 0 | } |
125 | |
|
126 | |
private void updateValueLabel() { |
127 | 0 | valueField.setText(sampleManager.getValue()); |
128 | 0 | } |
129 | |
|
130 | |
public void actionPerformed(ActionEvent e) { |
131 | |
try { |
132 | |
|
133 | 0 | if (e.getSource() == saveButton) { |
134 | 0 | sampleManager.setValue(valueField.getText()); |
135 | |
|
136 | 0 | } else if (e.getSource() == refreshButton) { |
137 | 0 | updateValueLabel(); |
138 | |
|
139 | 0 | } else if (e.getSource() == secureMethod1Button) { |
140 | 0 | sampleManager.secureMethod1(); |
141 | 0 | JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE); |
142 | |
|
143 | 0 | } else if (e.getSource() == secureMethod2Button) { |
144 | 0 | sampleManager.secureMethod2(); |
145 | 0 | JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE); |
146 | 0 | } else if (e.getSource() == secureMethod3Button) { |
147 | 0 | sampleManager.secureMethod3(); |
148 | 0 | JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE); |
149 | |
|
150 | |
} else { |
151 | 0 | throw new RuntimeException("Unexpected action event from source: " + e.getSource()); |
152 | |
} |
153 | |
|
154 | 0 | } catch (AuthorizationException ae) { |
155 | 0 | JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE); |
156 | 0 | } |
157 | 0 | } |
158 | |
} |